1

How can I output a custom field in Drupal 8 (Bootstrap theme)? Editing the field.html.twig file, if I have a custom field named field_title I thought

{{content.field_title}}

Would display it, but it doesn't. I've also tried:

{{item.content['#field_title']}}

To no avail.

The only thing that's worked for me is:

{{items[0]}}

But that picks up the custom field by its index number, instead of its associative name in the array.

What am I doing wrong?

1

{{ content.field_title }}

This should work if you've got a field with that machine name. If you're trying to print the title, the current syntax is:

{{ label }}

An easier way to figure out if the variables that you are trying to use is actually usable is to download the Devel module and enable Kint. Then in your .html.twig template file, you can enter:

{{ kint(content.field_myCustomfield) }}

and you would get an array with all the available variables. If you don't want to install the devel module, you can just use: I would recommend Devel/kint, it makes everything prettier and easier

{{ dump() }}

You're only going to need to dig into the array if you're trying to do something really custom. Eg: I used the following code in one of my projects to dig down into a link field so that i can output the exact url into an A tag inorder to wrap an entire region in a link.

<a href="{{ content.field_card_custom_link[0]['#plain_text'] }}">

You can read more about outputting Variables in twig templates here: https://sqndr.github.io/d8-theming-guide/twig/twig-debug.html

and on drupal.org here: https://www.drupal.org/theme-guide/8

1

I found out my problem was I was editing the wrong template file. The file I needed to edit was:

/themes/bootstrap_subtheme/templates/node/node.html.twig

Once I added {{ content.field_title }} there, it worked!

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.