Answers.Body: <p>I've used this method before, works quite nice!</p>
  
  <p>The way to do this:</p>
  
  <ul>
  <li>Create the fields you need for commenting (name, message, etc.). Be sure to add a relational field to which you can later assign the 'parent entry'</li>
  <li>Create a section (channel) called <code>comments</code> (or whatever else you want to call it)</li>
  <li>Assign the fields to the section you just created</li>
  <li>Use a <a href="http://buildwithcraft.com/docs/templating/entry-form" rel="noreferrer">frontend entry form</a> for commenting. The <code>sectionId</code> is the id of the comments section, the <code>action</code> is 'entries/saveEntry'.</li>
  <li><p>Create a hidden input field, with the name of your relational field and assign the id of the 'parent entry' to it. Don't forget to add <code>fields</code> and []. <code>Fields</code> because it's not a default field, the <code>[]</code> because it's a relational field. So:</p>
  
  <p><code>&lt;input type="hidden" name="fields[yourRelationalFieldName][]" value="{{ entry.id }}" /&gt;</code></p></li>
  </ul>
  
  <p>Now, when the user commits this form (and has the permissions to create new entries in the comments section), a new comments entry will be created, related to the parent entry. You can even relate comments to other comments this way.</p>
  
  <p>Good luck!</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-06T08:18:01.350Z
Answers.Text: Sorry Paul. Didn't mean to overwrite your answer. We were obviously posting at the same time. Good tag team effort ;)
Answers.UserDisplayName: Douglas McDonald
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-06T08:39:43.137Z
Answers.Text: No problem at all!
Answers.UserDisplayName: Paul
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-06T09:51:31.410Z
Answers.Text: Thanks again. Between the two answers here I think I have enough to get me up and running. I'll update here tonight when I've tested a bit more. I do think this way of commenting allows for a lot of flexibility.
Answers.UserDisplayName: shorn
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-06T07:57:58.753Z
Answers.LastActivityDate: 2014-10-06T07:57:58.753Z
Answers.UserDisplayName: Paul
Answers.Body: <p>This worked like a charm for me! Thanks if anyone needs the short code for the hidden input it's basically the same...</p>
  <pre><code>{{ hiddenInput('fields[yourRelationalFieldName][]', postId) }}
  </code></pre>
  <p>It took me a couple of tries to get it. The second <code>[]</code> is required like the post says.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-07-16T17:19:18.507Z
Answers.LastActivityDate: 2021-07-16T17:19:18.507Z
Answers.UserDisplayName: yankeyhotel
Answers.Body: <p>One approach would be to create a comments channel with an <code>entries</code> fieldtype (with sources checked for any channels that you want to allow commenting; limit set to 1) called 'relatedEntry' (or whatever you like). On your article template, include a <code>for</code> loop to display existing comments <code>relateTo</code> the entry. Check to see if there is a current user, and if yes, include a comments form. In the comments form place a hidden field with the id for the current entry. Something like this:</p>
  
  <pre><code>&lt;article&gt;
    {{ entry.title }}
    {{ entry.body }}
  &lt;/article&gt;
  
  &lt;ul class="comments"&gt;
  {% for comment in craft.entries.section('comments').relatedTo(entry) %}
     &lt;li&gt;{{ comment.author.fullName }}&lt;br/&gt;{{ comment.body }}&lt;/li&gt;
  {% endfor %}
  &lt;/ul&gt;
  
  {% if currentuser %}
  &lt;form method="post" action="" accept-charset="UTF-8"&gt;
      &lt;input type="hidden" name="action" value="entries/saveEntry"&gt;
      &lt;input type="hidden" name="sectionId" value="2"&gt;
      &lt;input type="hidden" name="redirect" value="{{ entry.url }}"&gt;
      &lt;input type="hidden" name="fields[relatedEntry][]" value="{{ entry.id }}"&gt;
  
      &lt;label for="comment"&gt;Comment&lt;/label&gt;&lt;br/&gt;
      &lt;input type="text" name="comment"&gt;
  &lt;/form&gt;
  {% endif %}
  </code></pre>
  
  <p><code>name="fields[relatedEntry][]</code> is formated this way because related fields expect an array even if it's only a single related entry.</p>
  
  <p>You could also create threaded comments by creating another related <code>entries</code> fieldtype called parentComment (or whatever you like) and including that as a hidden field in the form as well (you might need ajax or javascript here to help set the hidden field value from the 'comment' link).</p>
  
  <p>To make it a little nicer you could also load the comments form via ajax or reveal it with javascript.</p>
  
  <p>Untested, but hopefully this will point you in the right direction.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-05T18:06:07.603Z
Answers.Text: Hey @DouglasMcDonald, I'm just in the process of using this method for adding comments to entires and I've got it working, but I have one issue as I have more than one comment type field within the related entry, so if the user completes one of the fields one time and then goes back and completes the other comments field(s) it creates another entry.
  
  Is there anyway around this? Does it even make sense?
Answers.UserDisplayName: richardpixel
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-06T19:34:51.917Z
Answers.Text: @richardpixel - I'm not sure what you mean. What two comment fields do you need? Can you provide an example? Wouldn't both fields be part of the same comment? I.e. if the user went back and submitted the form again, wouldn't you want this to be it's own comment/entry? You could also potentially allow the ability for a user to edit (or in this case complete) their own comment by adding an edit button (and ajax loading a form preloaded with their previous field entries).
Answers.UserDisplayName: Douglas McDonald
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-07T10:55:38.193Z
Answers.Text: @DouglasMcDonald Thanks for your reply but I've just figured it out. What I needed to do was set a variable in my template to the related comments of the current user and then add that as a hidden field in the form, plus fill the comments fields with any previously entered content.
Answers.UserDisplayName: richardpixel
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-08T17:43:47.550Z
Answers.Text: @richardpixel glad you got it sorted!
Answers.UserDisplayName: Douglas McDonald
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-06T09:50:08.710Z
Answers.Text: This is brilliant. That's helped me get my head around it a bit better. I was (wrongly) thinking of how an entries field within the main entry rather than the comment section. I'm going to have a play tonight and test it out. As you mentioned, my aim would be to get the comments loading in via Ajax and somehow prevent the need for a page refresh, but that's refinement once I've got the basics in place. Will update once I've had a chance to test!
Answers.UserDisplayName: shorn
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-06T14:36:06.883Z
Answers.Text: I've ticked this as an answer but to be honest both answers here have helped. I have just got this up and running, and it's working pretty well. I have just two questions though. Should I set the comments section to have there own URLs? and also, I'm wondering what the best approach for generating the title of the comment entry is? I only want to present a single textarea and not have any other fields so auto generating one might be the best bet, but I'm open to suggestions as to the best way to set them up?
Answers.UserDisplayName: shorn
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-06T15:20:08.440Z
Answers.Text: You don't really need to have URLs for the comments channel unless you plan to have a template to view the comments separately from the article. And the title doesn't really matter. The title would just be there for you to identify it in the Control Panel. You could use something like `{{ postDate|date("m/d/Y H:i") }}` or `{{ relatedArticle[0].title }}` or `{{ relatedArticle[0].author.fullName }}` or some combination.
Answers.UserDisplayName: Douglas McDonald
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-06T17:51:17.177Z
Answers.Text: Thanks for that. I'm not planning on templating the comments separately but I guess it wouldn't hurt to generate a URL to future proof against potentially needing/deciding to in the future.
  Trying to think of what would be the most useful title for the CP. I tried your suggestion of `{{ postDate|date("m/d/Y H:i") }}` which works fine, but I'm not sure how `{{ relatedArticle[0].title }}` breaks down. I presume it should output the main entrys title, but can you explain the 0 within the `[]` as I'm not sure what that does. (New to Craft) Thanks again!
Answers.UserDisplayName: shorn
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-06T18:18:50.893Z
Answers.Text: Related entries are always an array, even if there is only one. And since we know that there is only one related entry because (hopefully) a limit of 1 is set in the field settings, we can reliably assume it is the first entry with an index of 0.
Answers.UserDisplayName: Douglas McDonald
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-06T20:10:36.780Z
Answers.Text: That makes sense, but when I add it to my auto title, nothing gets outputted. The date is generated but the title is not?
Answers.UserDisplayName: shorn
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-06T20:30:46.207Z
Answers.Text: My mistake... use `{ relatedArticle.first.title }`
Answers.UserDisplayName: Douglas McDonald
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-06T20:30:49.223Z
Answers.Text: Doh! Think I was having a moment. My entries field is `relatedEntry` rather than `relatedArticle` so I now have this `{relatedEntry[0].title} - {author}` and it outputs the main entry's title together with the comment authors username. Seems to work fine!
Answers.UserDisplayName: shorn
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-06T20:32:45.993Z
Answers.Text: Great! Guess either one works then.
Answers.UserDisplayName: Douglas McDonald
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-06T20:33:58.080Z
Answers.Text: Yep, tried both and both work. Thanks for your help on this. Really helped me along, and it's much appreciated!
Answers.UserDisplayName: shorn
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-06T08:08:41.210Z
Answers.LastActivityDate: 2014-10-06T08:08:41.210Z
Answers.UserDisplayName: Douglas McDonald
Body: <p>I've been reading on here about suggestions on implementing commenting systems. I'm aware of disqus integration but I want/need  built in commenting.</p>
  
  <p>I have seen it mentioned a couple of times that one approach would be to create a section (presumably a channel) dedicated to comments and the relate that to an entry via an entries field.</p>
  
  <p>I just can't quite wrap my head around how that works out. Do the entries have to be manually selected in order to relate them?</p>
  
  <p>If anyone's built something like this then I would love to know how they tackled it! </p>
  
  <p>One thing to add is that only registered members will be commenting. And there would be several different sections needing comments.</p>
  
  <p>Thanks!</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-08-03T17:55:26.883Z
Comments.Text: The problem I had with this method is that you need to be log in. Or may I miss something ?
Comments.UserDisplayName: rdomingue
Tags: <entries><relations><comments>
Title: Creating comments via a related section
ThreadText: # Creating comments via a related section
  
  (Asked by: username_0 on 2014-10-06)
  
  I've been reading on here about suggestions on implementing commenting systems. I'm aware of disqus integration but I want/need built in commenting.
  
  I have seen it mentioned a couple of times that one approach would be to create a section (presumably a channel) dedicated to comments and the relate that to an entry via an entries field.
  
  I just can't quite wrap my head around how that works out. Do the entries have to be manually selected in order to relate them?
  
  If anyone's built something like this then I would love to know how they tackled it!
  
  One thing to add is that only registered members will be commenting. And there would be several different sections needing comments.
  
  Thanks!
  
  **Tags:** entries, relations, comments
  
  > **Comments:**
  > **username_1**: The problem I had with this method is that you need to be log in. Or may I miss something ?  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_3 on 2014-10-06. Score: 8 | Accepted answer
  
  One approach would be to create a comments channel with an `entries` fieldtype (with sources checked for any channels that you want to allow commenting; limit set to 1) called 'relatedEntry' (or whatever you like). On your article template, include a `for` loop to display existing comments `relateTo` the entry. Check to see if there is a current user, and if yes, include a comments form. In the comments form place a hidden field with the id for the current entry. Something like this:
  
  ```
  <article>
    {{ entry.title }}
    {{ entry.body }}
  </article>
  
  <ul class="comments">
  {% for comment in craft.entries.section('comments').relatedTo(entry) %}
     <li>{{ comment.author.fullName }}<br/>{{ comment.body }}</li>
  {% endfor %}
  </ul>
  
  {% if currentuser %}
  <form method="post" action="" accept-charset="UTF-8">
      <input type="hidden" name="action" value="entries/saveEntry">
      <input type="hidden" name="sectionId" value="2">
      <input type="hidden" name="redirect" value="{{ entry.url }}">
      <input type="hidden" name="fields[relatedEntry][]" value="{{ entry.id }}">
  
      <label for="comment">Comment</label><br/>
      <input type="text" name="comment">
  </form>
  {% endif %}
  
  ```
  
  `name="fields[relatedEntry][]` is formated this way because related fields expect an array even if it's only a single related entry.
  
  You could also create threaded comments by creating another related `entries` fieldtype called parentComment (or whatever you like) and including that as a hidden field in the form as well (you might need ajax or javascript here to help set the hidden field value from the 'comment' link).
  
  To make it a little nicer you could also load the comments form via ajax or reveal it with javascript.
  
  Untested, but hopefully this will point you in the right direction.
  
  > **Comments:**
  > **username_0**: This is brilliant. That's helped me get my head around it a bit better. I was (wrongly) thinking of how an entries field within the main entry rather than the comment section. I'm going to have a play tonight and test it out. As you mentioned, my aim would be to get the comments loading in via Ajax and somehow prevent the need for a page refresh, but that's refinement once I've got the basics in place. Will update once I've had a chance to test!  
  > **username_0**: I've ticked this as an answer but to be honest both answers here have helped. I have just got this up and running, and it's working pretty well. I have just two questions though. Should I set the comments section to have there own URLs? and also, I'm wondering what the best approach for generating the title of the comment entry is? I only want to present a single textarea and not have any other fields so auto generating one might be the best bet, but I'm open to suggestions as to the best way to set them up?  
  > **username_3**: You don't really need to have URLs for the comments channel unless you plan to have a template to view the comments separately from the article. And the title doesn't really matter. The title would just be there for you to identify it in the Control Panel. You could use something like `{{ postDate|date("m/d/Y H:i") }}` or `{{ relatedArticle[0].title }}` or `{{ relatedArticle[0].author.fullName }}` or some combination.  
  > **username_0**: Thanks for that. I'm not planning on templating the comments separately but I guess it wouldn't hurt to generate a URL to future proof against potentially needing/deciding to in the future. Trying to think of what would be the most useful title for the CP. I tried your suggestion of `{{ postDate|date("m/d/Y H:i") }}` which works fine, but I'm not sure how `{{ relatedArticle[0].title }}` breaks down. I presume it should output the main entrys title, but can you explain the 0 within the `[]` as I'm not sure what that does. (New to Craft) Thanks again!  
  > **username_3**: Related entries are always an array, even if there is only one. And since we know that there is only one related entry because (hopefully) a limit of 1 is set in the field settings, we can reliably assume it is the first entry with an index of 0.  
  > **username_0**: That makes sense, but when I add it to my auto title, nothing gets outputted. The date is generated but the title is not?  
  > **username_3**: My mistake... use `{ relatedArticle.first.title }`  
  > **username_0**: Doh! Think I was having a moment. My entries field is `relatedEntry` rather than `relatedArticle` so I now have this `{relatedEntry[0].title} - {author}` and it outputs the main entry's title together with the comment authors username. Seems to work fine!  
  > **username_3**: Great! Guess either one works then.  
  > **username_0**: Yep, tried both and both work. Thanks for your help on this. Really helped me along, and it's much appreciated!  
  > **username_5**: Hey @username_3, I'm just in the process of using this method for adding comments to entires and I've got it working, but I have one issue as I have more than one comment type field within the related entry, so if the user completes one of the fields one time and then goes back and completes the other comments field(s) it creates another entry.  Is there anyway around this? Does it even make sense?  
  > **username_3**: @username_5 - I'm not sure what you mean. What two comment fields do you need? Can you provide an example? Wouldn't both fields be part of the same comment? I.e. if the user went back and submitted the form again, wouldn't you want this to be it's own comment/entry? You could also potentially allow the ability for a user to edit (or in this case complete) their own comment by adding an edit button (and ajax loading a form preloaded with their previous field entries).  
  > **username_5**: @username_3 Thanks for your reply but I've just figured it out. What I needed to do was set a variable in my template to the related comments of the current user and then add that as a hidden field in the form, plus fill the comments fields with any previously entered content.  
  > **username_3**: @username_5 glad you got it sorted!  
  
  ---
  
  ### Answer by username_2 on 2014-10-06. Score: 6
  
  I've used this method before, works quite nice!
  
  The way to do this:
  
  * Create the fields you need for commenting (name, message, etc.). Be sure to add a relational field to which you can later assign the 'parent entry'
  * Create a section (channel) called `comments` (or whatever else you want to call it)
  * Assign the fields to the section you just created
  * Use a [frontend entry form](http://buildwithcraft.com/docs/templating/entry-form) for commenting. The `sectionId` is the id of the comments section, the `action` is 'entries/saveEntry'.
  * Create a hidden input field, with the name of your relational field and assign the id of the 'parent entry' to it. Don't forget to add `fields` and []. `Fields` because it's not a default field, the `[]` because it's a relational field. So:
  
    `<input type="hidden" name="fields[yourRelationalFieldName][]" value="{{ entry.id }}" />`
  
  Now, when the user commits this form (and has the permissions to create new entries in the comments section), a new comments entry will be created, related to the parent entry. You can even relate comments to other comments this way.
  
  Good luck!
  
  > **Comments:**
  > **username_3**: Sorry username_2. Didn't mean to overwrite your answer. We were obviously posting at the same time. Good tag team effort ;)  
  > **username_2**: No problem at all!  
  > **username_0**: Thanks again. Between the two answers here I think I have enough to get me up and running. I'll update here tonight when I've tested a bit more. I do think this way of commenting allows for a lot of flexibility.  
  
  ---
  
  ### Answer by username_4 on 2021-07-16. Score: 0
  
  This worked like a charm for me! Thanks if anyone needs the short code for the hidden input it's basically the same...
  
  ```
  {{ hiddenInput('fields[yourRelationalFieldName][]', postId) }}
  
  ```
  
  It took me a couple of tries to get it. The second `[]` is required like the post says.
  
  
  ---
  
  