2

Anonymous users can leave their e-mail when posting a comment but how can I display this e-mail as a plain text value in the comment.html.twig?

5
  • By default, anonymous comments have no such "e-mail" field. Was this field added by you?
    – No Sssweat
    Commented Dec 19, 2017 at 10:14
  • If yes and you used the e-mail field type, then simply go to the comments manage display and for Format select plain text.
    – No Sssweat
    Commented Dec 19, 2017 at 10:23
  • You can enable anonymous users to leave their contact information in the comments field of a content type.
    – rroose
    Commented Dec 19, 2017 at 18:56
  • Where is that option located at?
    – No Sssweat
    Commented Dec 19, 2017 at 23:11
  • 1
    If you have a fresh Drupal 8 standard install, go to: 'People > Permissions' and Allow anonymous users to 'Post comments' and 'Skip comment approval'. Then go to: 'Structure > Content Types > Article > Manage Fields' and edit the 'Comments' field. Go to 'Anonymous commenting' and select 'Anonymous posters may leave their contact information'. Click 'Save settings'. Now when you go post a comment as an anonymous user you can leave your e-mail.
    – rroose
    Commented Dec 20, 2017 at 8:18

1 Answer 1

2

If you have a fresh Drupal 8 standard install, go to: 'People > Permissions' and Allow anonymous users to 'Post comments' and 'Skip comment approval'. Then go to: 'Structure > Content Types > Article > Manage Fields' and edit the 'Comments' field. Go to 'Anonymous commenting' and select 'Anonymous posters may leave their contact information'. Click 'Save settings'. Now when you go post a comment as an anonymous user you can leave your e-mail.

You can print that e-mail value in plain text like this:

{{ content.comment_body['#object'].getAuthorEmail() }}

Now, this will print the e-mails of logged in users as well. If you don't want that, then you will need to add a conditional to check if their User ID is 0, which means they are anonymous.

{% if attributes.toArray()['data-comment-user-id'] == 0 %}

    {{ content.comment_body['#object'].getAuthorEmail() }}

{% endif %}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.