I have an email field that I would like to present on the node as a name that links to the email address. As such, I am trying to edit the email field template to embed the name from a text field. However, I'm having difficulty figuring out how to access the content of this text field. I've gotten as far as figuring out that twig(_context['element']['#object']) will give me some info about the node object, but I cannot figure out how to access the field values. Am I even in the right ballpark for accessing this data?

  • Use preprocess function to get to the node object from the field(i think it's either getParent() or getEntity() method) and either put the whole node into the variables or just the field you want to access...or just the value of course. – user21641 Oct 23 '16 at 13:57
  • As of drupal 8.4+ the following debug string will show properties of the node in a field html twig file: {{ dump(_context['element']['#object']) }} Note that twig debugging should be enabled in the sitewide services.yml file. – HongPong Nov 10 '17 at 16:55

This is the node object that you have found in the field template.

You can access the field values like this:

{{ element['#object'].field_field1.value }}
  • Perfect, thanks! Do you have any good resources for this aspect of theming in D8? – Mrweiner Oct 23 '16 at 21:02

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.