0

I succesfully get user name, surname and date to render in comment.html.twig like this

function MYTHEMENAME_preprocess_comment(&$variables) {
    $comment = $variables['comment'];
    $variables['user_name_comment'] = $comment->getOwner()->field_first_name->value;
    $variables['user_surname_comment'] = $comment->getOwner()->field_last_name->value;
    $variables['created'] = \Drupal::service('date.formatter')->format($variables['comment']->getCreatedTime(), 'conference_date');
}

In node template i got field_picture to render like this

$variables['author_picture'] = $node->getOwner()->field_picture->entity->url();

But i can't get my field_picture from user to render in comment twig file. I try to show the author's picture of every comment.

Any ideas?

  • Try: $variables['author_picture'] = file_create_url($node->getOwner()->field_picture->entity>getFileUri()); – lamp5 Jul 23 '18 at 9:58
  • nope. This breaks the page. – tpliakas Jul 23 '18 at 10:06
0

When you want to display an entity field based on the "Manage Display" settings, you should go just {{ content.my_field }}. If you "kint" content (var dump it) you can see that each content field element has also "theme" key/property, which tells twig how to render that field

  • I have seen this but it doesn't seem to work. I tried also user_picture and this isn't rendering too. Btw, my settings looking grayed out in theme settings (User pictures in posts & User pictures in comments checkboxes. Both checked but i can't uncheck them). – tpliakas Jul 23 '18 at 10:10
  • have you tried to load user object in preprocessing and pass user picture URL in the variable? – Shamsher Alam Jul 23 '18 at 10:22
  • that would bring me the image of the current user. Right? Not the owner of the comment. – tpliakas Jul 23 '18 at 10:28
  • every comment object having author id so you can load author and fetch author detail in a variable. – Shamsher Alam Jul 23 '18 at 10:29
  • actually the comment object is loading. i am able to print author id with {{ comment.getOwnerId() }} without preprocess, but still i can't print the picture.. :( – tpliakas Jul 23 '18 at 10:41

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.