6

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?

2
  • 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)? Aug 28 '17 at 8:26
  • fields added to a custom blocks type. Aug 28 '17 at 9:16
3

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 :)

11
  • 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. Aug 28 '17 at 9:14
  • 1
    there is a lot of output. which part specifically should i be looking for? Aug 28 '17 at 9:34
  • 1
    It's likely that the fields are not configured in the display (Manage Display) on the block type.
    – Kevin
    Aug 28 '17 at 15:07
  • 1
    the fields are configured correctly in Manage Display. They appear fine with {{ content }}. It is my understanding after much research getting individual field values from custom blocks is not really possible in D8. Aug 29 '17 at 12:39
  • 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.