I try to render the last comment from a list of comments.

I try {{ comments|last }} ---> no result.

Then {{ comments|reverse }} ---> this render them

Then {{ comments|reverse|first }} ---> no result

The above part is solved.See anser 1. Pls if annyone have a solution for rendering the last comment in views.

I like to achieve the same in a view. There is a relation filter that output the last comment. But I think I do something wrong.

  • You don't provide details in which template you are exactly, but what you have here might be the product of viewMultiple(), where the childrens are not ready for rendering yet and you need to provide a prerender callback, see drupal.stackexchange.com/questions/263158/…
    – 4k4
    Aug 15 '18 at 8:55
  • I use the field--comment.html.twig file from Stable in core, if I debug, this template is resposable to render the commenst
    – Belba
    Aug 15 '18 at 9:16

The last item in comments is the pager, which you have to remove first to get to the last comment. Then you can build a new render array with the single comment and the pre-render callback:

{% set last_comment = comments|without('pager')|last %}
{% set pre_render = comments['#pre_render'] %}
{% set last_comment_build = { '#pre_render': pre_render, '0': last_comment } %}
{{ last_comment_build }}
  • This indeed works. However I dont understand the entire code. Wy the '0' . I also dont understand how you see that this last item is the pager. The view mode still render all the comments. Thanks.
    – Belba
    Aug 15 '18 at 11:48
  • The '0' is arbitrary, could be anything not starting with a #. You can look into comments, if there is no pager then you don't need |without(),but it doesn't harm either.
    – 4k4
    Aug 15 '18 at 12:00

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.