8

I have a template for my field: field--field-category.html.twig. Is there a way to override this for a specific Display Mode? A have a mode called teaser block. I tried field--teaser-block--field-category.html.twig as well as a couple of other formats.

I know I can modify how the field is output in node--teaser-block.html.twig but I'd like to do it at the field template level.

1

1 Answer 1

15

For the field template there is by default no suggestion with the specific display mode.

You can add this suggestion in a hook:

function MYTHEME_theme_suggestions_field_alter(&$suggestions, $variables) {
  $suggestions[] = 'field__' . 
    $variables['element']['#field_name'] . '__' . 
    $variables['element']['#view_mode'];
}

Put this into MYTHEME.theme in the theme directory.

1
  • 2
    Good stuff. But I think to stick with D8's naming convention, you should really make it $suggestions[] = 'field__node__' . $variables['element']['#field_name'] . '__' . $variables['element']['#bundle'] . '__' . $variables['element']['#view_mode']; Aug 23, 2018 at 17:00

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.