The list of theme hook suggestions for "field" is incorrectly ordered and has namespacing collisions

Created on 11 December 2011, over 13 years ago
Updated 5 August 2025, 4 days ago

Postponed on #3159050: Allow deprecating theme suggestions

Problem/Motivation

Currently, core has 4 theme hook suggestions for the "field" theme hook:

field--[field-type].tpl.php
field--[field-name].tpl.php
field--[bundle].tpl.php 
field--[field-name]--[bundle].tpl.php

Note: that the field name is prefixed with the name of the module providing the field, which usally means “field-” is prefixed to the field name. Also, note that the "bundle" is a sub-type of an entity, e.g. “content types” are bundles since an "article" is a bundle of the node entity.

The theme hook suggestions are supposed to be in order of specificity. With least specific suggestions appearing first in the list. Suggestions at the end of the list override suggestions earlier in the list.

Unfortunately, the field--[field-name].tpl.php and field--[bundle].tpl.php suggestions are in the wrong order. A specific field should take precedence over "any field attached to this bundle". A fix for this should be backported to D7.

We also have a namespacing issue. If the same name is used for the bundle name or the field name or the field type, it can cause unintended issues. For example, if a user creates an "image" content type there will be a namespace collision with any fields that are the "image" field type; field--[field-type] and field--[bundle] would both be field--image. If a themer wanted to use a theme hook suggestion for all image-type fields (perfectly reasonable!), they would also accidentally trigger the bundle-based suggestion on all fields attached to the image content type (perfectly horrible!)

Proposed resolution

Fix the order of specificity for theme hook suggestions, namely:

field--[field-type].tpl.php
field--[bundle].tpl.php 
field--[field-name].tpl.php
field--[field-name]--[bundle].tpl.php

Come up with a way to properly namespace theme hook suggestions so that bundle-based suggestions don't conflict with type-based suggestions. Note that field names are currently prefixed with the name of the module implementing the field, which almost universally means a field name is prefixed with "field-".

Remaining tasks

  1. Reach consensus on a namespacing fix.
  2. Write a Drupal 8 patch.
  3. Backporting the namespacing fix would break D7 themes in the wild, so its not backportable. But, the order of specificity fix should be backported to D7.
  4. Workaround

    See here for a workaround that provides a solution for adding theme suggestions for fields based on display mode: https://drupal.stackexchange.com/questions/201210/field-template-for-dis...

🐛 Bug report
Status

Needs work

Version

11.0 🔥

Component

theme system

Created by

🇺🇸United States RobW

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024