1

For context: in my template file node--some-node.html.twig i have a grid of items which have a custom comment form on them. The form creates a comment through an API. The form passes the ID of the grid item it is under to the API and the created comment then has a field_profile_id with the item's id in it. Now, in the template file I need to output JUST the comments that belong to that grid item.

When I write this into the template file: {{ content.field_comments }} i get the output of all the comments that belong to this node and underneath I get the comment form (which I don't need). Ideally I would like to do everything in the same template file, so I could do something like this: {% if entity.nid.value == content.field_comments.someComment.field_profile_id %} {# output the comment here #} {% endif %}

Here the entity.nid.value is the node id of the grid items I was talking about earlier.

EDIT: Theoretically what I need to do is:

  • Iterate through all possible comments for this node.
  • Check if comment has some ID value (that a grid item might have)
  • If the IDs match, print out the comment, if it doesn't skip it
1

You must add a file to your template called

field--comment.html.twig

Use the following code.

{#
/**
 * @file
 * Theme override for comment fields.
 *
 * Available variables:
 * - attributes: HTML attributes for the containing element.
 * - label_hidden: Whether to show the field label or not.
 * - title_attributes: HTML attributes for the title.
 * - label: The label for the field.
 * - title_prefix: Additional output populated by modules, intended to be
 *   displayed in front of the main title tag that appears in the template.
 * - title_suffix: Additional title output populated by modules, intended to
 *   be displayed after the main title tag that appears in the template.
 * - comments: List of comments rendered through comment.html.twig.
 * - comment_form: The 'Add new comment' form.
 * - comment_display_mode: Is the comments are threaded.
 * - comment_type: The comment type bundle ID for the comment field.
 * - entity_type: The entity type to which the field belongs.
 * - field_name: The name of the field.
 * - field_type: The type of the field.
 * - label_display: The display settings for the label.
 *
 * @see template_preprocess_field()
 * @see comment_preprocess_field()
 */
#}
{%
  set classes = [
    'field',
    'field--name-' ~ field_name|clean_class,
    'field--type-' ~ field_type|clean_class,
    'field--label-' ~ label_display,
    'comment-wrapper',
  ]
%}
{%
  set title_classes = [
    'title',
    label_display == 'visually_hidden' ? 'visually-hidden',
  ]
%}
<section{{ attributes.addClass(classes) }}>
  {% if comments and not label_hidden %}
    {{ title_prefix }}
    <h2{{ title_attributes.addClass(title_classes) }}>{{ label }}</h2>
    {{ title_suffix }}
  {% endif %}

  {{ comments }}

  {% if comment_form %}
    <h2 class='title comment-form__title'>{{ 'Add new comment'|t }}</h2>
    {{ comment_form }}
  {% endif %}

</section>
  • Hello, I am not really sure how this would help. Vaguely speaking what I need to do is: iterate through comments and check if a comment has a certain ID value. If it does, print it, if it doesn't don't. I will update my question – Mantas Nov 11 '19 at 21:14
0

So the node in question had a field field_comments, from which I was trying to access the comments. I found a different way of doing that - I created a MYMODULE_preprocess_template function, loaded all comments of the node in that function, created a variable "comments", where I stored the comment data I needed and passed it on to the twig template. Here's the code in MYMODULE_preprocess_template function:

$node = \Drupal::routeMatch()->getParameter('node');


$user_comments = [];
  $entity_manager = \Drupal::entityTypeManager();
  try{
    $storage = $entity_manager->getStorage('comment');
    $comment_field = $node->get('field_comments');
    $comments = $storage->loadThread($node, $comment_field->getFieldDefinition()->getName(),\Drupal\comment\CommentManagerInterface::COMMENT_MODE_FLAT);
    if(empty($comments)){
      return;
    }
    foreach($comments as $comment) {
  $user_comments[] = [
    'cid' => $comment->id(),
    'user_id' => $comment->get('field_prf')->value,
    'comment' => $comment->get('field_comment')->value,
    'alias' => $comment->get('field_alias')->value,
    'rating' => $comment->get('field_rating')->value
  ];
}

  } catch (\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException $e) {
  } catch (\Drupal\Component\Plugin\Exception\PluginNotFoundException $e) {
  }
  $variables['user_comments'] = $user_comments;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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