Skip to content

Commit 4e11c5f

Browse files
authored
Add options to Comments and Comment Forms to allow submission (#1498)
## Overview This makes some changes to our components to allow the Wordpress site to properly set up comment form submission. I'm like 90% sure this will be all we need. There's a chance we'll need to do a follow-up PR if things don't work as expected in WP land. ### Comment Form The comment form got a new `hidden_inputs` block which allows you to pass in `<input type="hidden">` elements, which Wordpress requires for comment submission. (We technically could have done this already with the `prompt` block, but that felt kinda gross and I had to make other changes.) ### Comment This component required more changes so that it can configure its nested Comment Form: #### Properties - `reply_form_action`: the action for the reply form - `reply_form_method`: the method for the reply form #### Blocks - `reply_form_hidden_inputs`: This allows you to pass in content to the form's new `hidden_inputs` block - `reply_form`: This allows you to replace the whole reply form. We shouldn't currently need this but it seemed like a useful escape hatch.
1 parent d21a0d6 commit 4e11c5f

File tree

5 files changed

+49
-12
lines changed

5 files changed

+49
-12
lines changed

.changeset/mean-spies-glow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@cloudfour/patterns': minor
3+
---
4+
5+
Add new options to Comment and Comment Form components to support comment submission

src/components/comment-form/comment-form.stories.mdx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,11 @@ There is also a reply version of this component (`is_reply: true`), used to repl
129129

130130
- `log_out_url`: URL used for log out link.
131131
- `is_reply`: Whether the comment is a reply to another comment.
132-
- `prompt` (block): The text that is displayed above the messge box.
132+
133+
## Template blocks
134+
135+
- `prompt`: The text that is displayed above the message box.
136+
- `hidden_inputs`: Can be used to include `<input type="hidden">` elements, which systems like Wordpress use to provide additional context during form submission.
133137

134138
## JavaScript
135139

src/components/comment-form/comment-form.twig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@
2525
{% endblock %}
2626
{% endembed %}
2727
{% endblock %}
28+
29+
{#
30+
Can be used to include `<input type="hidden">` elements, which systems like
31+
Wordpress use to provide additional context during form submission.
32+
#}
33+
{% block hidden_inputs %}{% endblock %}
34+
2835
{% embed '@cloudfour/objects/form-group/form-group.twig' with { label: main_label|default('Message') } %}
2936
{% block control %}
3037
{% include '@cloudfour/components/input/input.twig' with {

src/components/comment/comment.stories.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,13 +299,17 @@ While it is theoretically possible to infinitely nest `children`, it's recommend
299299
```
300300
- `log_out_url`: URL used for log out link.
301301
- `source`: An optional object containing a `url` and `name` for the comment source.
302+
- `reply_form_action`: A form action for the comment reply form
303+
- `reply_form_method`: A form method for the comment reply form
302304

303305
## Template Blocks
304306

305307
- `header_content`: Use to over-write the content of the comment header
306308
- `heading_content`: Use to over-write the content of the comment heading
307309
- `author_title`: Use to over-write a portion of the comment heading content. Will be followed by a visually hidden span reading either `replied` or `said`. (Valid values could be `John Doe` or `John Doe (Post Author)`.)
308310
- `replies`: Use to pass in your own replies markup. This is useful if you need to modify other blocks within replies.
311+
- `reply_form`: Can be used to replace the entire reply form
312+
- `reply_form_hidden_inputs`: Can be used to add hidden inputs to the comment reply form. This can be useful for integrating with systems like Wordpress which use these provide additional context during form submission.
309313

310314
## JavaScript Instructions
311315

src/components/comment/comment.twig

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,18 +94,35 @@
9494
{% set _section_heading_depth = min(_heading_depth + 1, 6) %}
9595

9696
{% if allow_replies %}
97+
{#
98+
This is defined here, so we can pass it into the block of a nested embed.
99+
@see https://benfurfie.co.uk/articles/how-to-nest-a-block-in-another-block-in-an-embed-in-twig
100+
#}
101+
{% set reply_form_hidden_inputs_content %}
102+
{% block reply_form_hidden_inputs %}{% endblock %}
103+
{% endset %}
104+
97105
<div class="c-comment__reply-form js-comment-reply-form">
98-
{% include '@cloudfour/components/comment-form/comment-form.twig' with {
99-
is_reply: true,
100-
logged_in_user: logged_in_user,
101-
log_out_url: log_out_url,
102-
heading_id: "reply-to-comment-#{comment.ID}",
103-
help_text_id: "reply-to-comment-#{comment.ID}-help-text",
104-
heading_tag: "h#{_section_heading_depth}",
105-
heading_text: "Reply to #{comment.author.name}",
106-
heading_class: 'u-hidden-visually',
107-
main_label: 'Reply',
108-
} only %}
106+
{% block reply_form %}
107+
{% embed '@cloudfour/components/comment-form/comment-form.twig' with {
108+
is_reply: true,
109+
logged_in_user: logged_in_user,
110+
log_out_url: log_out_url,
111+
heading_id: "reply-to-comment-#{comment.ID}",
112+
help_text_id: "reply-to-comment-#{comment.ID}-help-text",
113+
heading_tag: "h#{_section_heading_depth}",
114+
heading_text: "Reply to #{comment.author.name}",
115+
heading_class: 'u-hidden-visually',
116+
main_label: 'Reply',
117+
action: reply_form_action,
118+
method: reply_form_method,
119+
reply_form_hidden_inputs_content: reply_form_hidden_inputs_content
120+
} only %}
121+
{% block hidden_inputs %}
122+
{{reply_form_hidden_inputs_content}}
123+
{% endblock %}
124+
{% endembed %}
125+
{% endblock %}
109126
</div>
110127
{% endif %}
111128
{% if comment.children is not empty %}

0 commit comments

Comments
 (0)