From 883ac88bdaa4b36e804e9fb6aa9a7d137eaeff19 Mon Sep 17 00:00:00 2001 From: Rohan Shaw Date: Sun, 1 Jun 2025 00:06:35 +0530 Subject: [PATCH 1/3] fix(comment): Enhance comment form with dynamic labels and reply descriptions --- plone/app/discussion/browser/comments.pt | 82 +++++++++++++++++++++++- plone/app/discussion/browser/comments.py | 80 ++++++++++++++++++++++- 2 files changed, 157 insertions(+), 5 deletions(-) diff --git a/plone/app/discussion/browser/comments.pt b/plone/app/discussion/browser/comments.pt index 291512eb..ec0b49eb 100644 --- a/plone/app/discussion/browser/comments.pt +++ b/plone/app/discussion/browser/comments.pt @@ -288,9 +288,14 @@
- Add comment - -

+ Add a new comment + +

You can add a comment by filling out the form below. Plain text formatting.

@@ -299,5 +304,76 @@
+ + + + diff --git a/plone/app/discussion/browser/comments.py b/plone/app/discussion/browser/comments.py index 0de2ab03..ac2ed5be 100644 --- a/plone/app/discussion/browser/comments.py +++ b/plone/app/discussion/browser/comments.py @@ -53,6 +53,27 @@ "transformed into clickable links.", ) +# New reply descriptions +COMMENT_DESCRIPTION_PLAIN_TEXT_REPLY = _( + "comment_description_plain_text_reply", + default="You can reply to this comment by filling out the form below. " + "Plain text formatting.", +) + +COMMENT_DESCRIPTION_MARKDOWN_REPLY = _( + "comment_description_markdown_reply", + default="You can reply to this comment by filling out the form below. " + "Plain text formatting. You can use the Markdown syntax for " + "links and images.", +) + +COMMENT_DESCRIPTION_INTELLIGENT_TEXT_REPLY = _( + "comment_description_intelligent_text_reply", + default="You can reply to this comment by filling out the form below. " + "Plain text formatting. Web and email addresses are " + "transformed into clickable links.", +) + COMMENT_DESCRIPTION_MODERATION_ENABLED = _( "comment_description_moderation_enabled", default="Comments are moderated.", @@ -62,7 +83,14 @@ class CommentForm(extensible.ExtensibleForm, form.Form): ignoreContext = True # don't use context to get widget data id = None - label = _("Add a comment") + + @property + def label(self): + """Dynamic label based on whether this is a reply or new comment.""" + if self.is_reply(): + return _("Reply here (this thread)") + else: + return _("Add a new comment") fields = field.Fields(IComment).omit( "portal_type", "__parent__", @@ -79,6 +107,11 @@ class CommentForm(extensible.ExtensibleForm, form.Form): # See https://github.com/plone/Products.CMFPlone/issues/3623 enable_autofocus = False + def is_reply(self): + """Check if this form is being used to reply to an existing comment.""" + in_reply_to = self.request.get('form.widgets.in_reply_to', None) + return bool(in_reply_to) + def updateFields(self): super().updateFields() self.fields["user_notification"].widgetFactory = SingleCheckBoxFieldWidget @@ -383,7 +416,7 @@ def comment_transform_message(self): registry = queryUtility(IRegistry) settings = registry.forInterface(IDiscussionSettings, check=False) - # text transform setting + # text transform setting - always use new comment descriptions for main form if settings.text_transform == "text/x-web-intelligent": message = translate( Message(COMMENT_DESCRIPTION_INTELLIGENT_TEXT), context=self.request @@ -417,6 +450,49 @@ def comment_transform_message(self): return message + def get_reply_transform_message(self): + """Returns the description for reply forms, + dependent on the text_transform setting and the comment moderation + workflow in the discussion control panel. + """ + context = aq_inner(self.context) + registry = queryUtility(IRegistry) + settings = registry.forInterface(IDiscussionSettings, check=False) + + # text transform setting - use reply descriptions + if settings.text_transform == "text/x-web-intelligent": + message = translate( + Message(COMMENT_DESCRIPTION_INTELLIGENT_TEXT_REPLY), context=self.request + ) + elif settings.text_transform == "text/x-web-markdown": + message = translate( + Message(COMMENT_DESCRIPTION_MARKDOWN_REPLY), context=self.request + ) + else: + message = translate( + Message(COMMENT_DESCRIPTION_PLAIN_TEXT_REPLY), context=self.request + ) + + # comment workflow + wftool = getToolByName(context, "portal_workflow", None) + workflow_chain = wftool.getChainForPortalType("Discussion Item") + if workflow_chain: + comment_workflow = workflow_chain[0] + comment_workflow = wftool[comment_workflow] + # check if the current workflow implements a pending state. If this + # is true comments are moderated + if "pending" in comment_workflow.states: + message = ( + message + + " " + + translate( + Message(COMMENT_DESCRIPTION_MODERATION_ENABLED), + context=self.request, + ) + ) + + return message + def has_replies(self, workflow_actions=False): """Returns true if there are replies.""" if self.get_replies(workflow_actions) is not None: From 6ed2836f84c3089512380fba71619e4b426f02fd Mon Sep 17 00:00:00 2001 From: Rohan Shaw Date: Sun, 1 Jun 2025 00:09:24 +0530 Subject: [PATCH 2/3] changelog --- news/88.bugfix | 1 + 1 file changed, 1 insertion(+) create mode 100644 news/88.bugfix diff --git a/news/88.bugfix b/news/88.bugfix new file mode 100644 index 00000000..7dfc2995 --- /dev/null +++ b/news/88.bugfix @@ -0,0 +1 @@ +Fixed mishandling of dynamic form label updates - New Comment & Thread @rohnsha0 \ No newline at end of file From 4cd2ca73c0639ac2bd6ce5f2626baef0e4a27940 Mon Sep 17 00:00:00 2001 From: Rohan Shaw Date: Sun, 1 Jun 2025 08:32:07 +0530 Subject: [PATCH 3/3] refactor(comment): lint --- plone/app/discussion/browser/comments.pt | 46 +++++++++++++----------- plone/app/discussion/browser/comments.py | 6 ++-- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/plone/app/discussion/browser/comments.pt b/plone/app/discussion/browser/comments.pt index ec0b49eb..a3ae67e2 100644 --- a/plone/app/discussion/browser/comments.pt +++ b/plone/app/discussion/browser/comments.pt @@ -288,14 +288,18 @@
- Add a new comment + data-reply-text="Reply here (this thread)" + i18n:translate="label_add_comment" + >Add a new comment -

+ tal:attributes=" + data-reply-html view/get_reply_transform_message; + " + > You can add a comment by filling out the form below. Plain text formatting.

@@ -309,38 +313,38 @@