How can I include the entity id in views output when rendering entities? I have a jQuery script that further processes each entity, but for doing so, I need to know the entity id. So I need to include it somehow in the rendered output. How can this be done?


In case of field display:

  • an field "node_id" that takes node id value ( you can hide the output ).
  • an field that can use "Rewrite the output of this field" with an output like: <div class="entity_{{ nid }}">something</div>

In case of entity display:

  • override node--yourcontenttype–teaser.html.twig, and replace {{ content }} with: <div class="entity_{{ entity.id.value }}">{{ content }}</div>

Then you can use jquery to handle the div with respective class or id.

  • This works for showing fields, but I am using the whole entity in views, so that I can't add additional fields, at least I don't know how. – Koval Fly Feb 16 '18 at 16:25
  • although this does not work for me as I use the entity (see above) how can I hide the output in your first suggestion? – Koval Fly Feb 16 '18 at 16:46
  • Sorry, now i've seen that you want to show the whole entity. In this case you can use twig to style the display type of your entity. Use twig_field_value module to display the node_id inside your twig template. – AlexB Feb 16 '18 at 17:46
  • I've edited the answer according to your needs, hope it helps – AlexB Feb 16 '18 at 17:57

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.