Theme colour switcher

Template suggestion for field templates per entity type - Drupal 8 & 9

The following template suggestion (placed in your theme's .theme file) will give you field template suggestions per entity type, e.g. field--node.html.twig, field--group.html.twig, field--media.html.twig, etc. I wanted to theme all fields in Groups the same way, and didn't want to have to create field templates for each group type, which was the least granular template suggestion available.

This is probably more general than most use cases will ever require, but it might be useful to someone somewhere.

/**
Add field templates per entity bundle
Example: field--node.html.twig, field--media.html.twig
Implements hook_theme_suggestions_field_alter().
*/
function THEMENAME_theme_suggestions_field_alter(&$suggestions, array $variables) {
  $entity_type = $variables['element']['#entity_type'];
  $suggestions[] = 'field__' . $entity_type;
}

Tags:

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.