I have tried as many options as I can imagine but none of them prints anything except:

{{ content }}

However, I require individual fields for more control over theming.

I am using a custom block type and the following function.

function THEME_theme_suggestions_block_alter(array &$suggestions, array $variables) {
  // Block suggestions for custom block bundles.
    if (isset($variables['elements']['content']['#block_content'])) {
    array_splice($suggestions, 1, 0, 'block__bundle__' . $variables['elements']['content']['#block_content']->bundle());

The template is working fine. I can hard code some random text and it is displayed. I just can't display the field values.

How do I print an individual field in block.html.twig?

  • What kind of individual fields do you need? Are they part of the block content (like a displayed node in a block) or part of the block itself (like its title)? – Drupal de Comptoir Aug 28 '17 at 8:26
  • fields added to a custom blocks type. – drupalstrap Aug 28 '17 at 9:16

If you have a field called field_test you schould be able to do like this

{{ content.field_test }}

and if you want to only print the value you can do like:

{{ content.field_test[0] }}

If you want to see what is inside the field you can do like this:

{{ kint(content.field_test) }}

But only if you have Devel module installed and have Devel Kint enabled you should be able to do that.

Hope this helps :)

  • 1
    thank you for your reply. while these options work in node.html.twig they do not work in block.html.twig for fields added to a custom block. this includes core fields like body. for example {{ content.body }} does not work. – drupalstrap Aug 28 '17 at 9:14
  • 1
    there is a lot of output. which part specifically should i be looking for? – drupalstrap Aug 28 '17 at 9:34
  • yep this solution is a pretty good one, clear your caches and try with something like that to get a lighter result. <ol> {% for key, value in _context %} <li>{{ key }}</li> {% endfor %} </ol> – Drupal de Comptoir Aug 28 '17 at 9:57
  • field_image and body. i checked in the kint arrays for these fields and i do not see any reference as far as how to display value from the template file – drupalstrap Aug 28 '17 at 10:07
  • 1
    Its entirely possible, I've done it. {{ content.field_name }}. – Kevin Aug 29 '17 at 13:09

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.