0

I activated the devel and devel kint modules. I also activated twig debugging, so, I can know which are templates used by Drupal.

So, I created a content-type named album that contains a field named tracks.

Now, I want customize this content type, so I created node--album.html.twig in templates folder of bartik, which works so I can begin to customize this template.

Now, I have to know which are variables used by this template. In comment section of node--album.html.twig I can read that content variable can be used to display content of page. I also read I can use subset of content, such as content.field_example to display a field.

OK, so if I want to modify rendering of field_tracks I can use content.field_tracks. Before using this variable I wanted to know all content variable, so I've written [[ kint(content) ]] in content section of this file. I rebuilt the cache, and I seen that content is an array, not an object as suggested in comment section of node file.

So, if I print [[ content.field_tracks ]] it returns nothing.

If I use kinkt() without parameters it breaks drupal and point me to core/install.php.

The same as I use d() without parameters.

At this point I am confused: there is a way to modify node--type twig templates without creating a file for each field?

2
  • [[ ]] is not valid twig syntax, you need to use curly brackets {{ content.field_tracks }}. Make sure you flush the cache to see changes. Alternatively, this should also work {{ node.field_tracks.value }}
    – No Sssweat
    Feb 11, 2018 at 12:41
  • You also don’t want to cherry pick through values in the render array, let it render.
    – Kevin
    Feb 11, 2018 at 14:29

1 Answer 1

1

Here is some examples

Render the whole field markup

{{ content.field_text }}

Get just the value of the field

{{ content.field_text.value }}

Get a field value from authors object

{{node.uid.entity.field_gender.value}}

For field above you can get the machine name of the field as you see it for example in admin/structure/types/manage/page/fields

For multivalue fields look at some examples here

Read more about how to find more variables here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.