3

I'm trying to display the values from the custom fields that have created on my Media Entity to my page--media.html.twig file. The fields that I'm trying to output are Location and Photographer

I tried using this code below, to make it work.

{{ node.field_photographer.entity.value }}

Also tried an alternative

{{ content.field_photographer.entity.value }}

enter image description here

  • If you're trying to access it from a page-level twig template, you'll need to add a preprocess hook to make those variables available to the template. You can use the Devel module and Devel Kint submodule to output what variables are currently available at the page-level template and debug/confirm your preprocess hook works. – Anson W Han Dec 15 '17 at 5:11
  • as @AnsonWHan said, probably you are in the wrong template to access the variables. You can check the available Variables in all Twig-Files easy. Do you need help for the preprocess hook to get the variables in the page-template? – btemperli Dec 15 '17 at 6:34
  • @btemperli I wasn't aware that I need to use a preprocess_hook in order to achieve this. I'm still new to drupal 8 – Noel Dec 15 '17 at 7:25
3

Your question is more about how to handle Drupal templates and passed variables. I suggest you to use Devel and Devel Kint modules to be able to debug variables just in your template like this:

{{ kint() }} - print all available variables

{{ kint(your_var_name) }} - print particular variable

And don't forget to enable twig debug mode - it will show you templates structure.

Once you have everything visible it becomes much easier to understand.

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.