Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I need to theming a cck field for particular content type.
Field: field_photo
Content type: events
For example, for a field named 'body' displayed on the 'article' content type, any of the following templates will override this default implementation. The first of these templates that exists is used:
field--body--article.html.twig
field.html.twig - work
field--field_photo.html.twig - work
field--field_photo--events.html.twig - it doesn't work.
This is a bug D8 or i do something wrong? Cache I cleared.
Comment | File | Size | Author |
---|---|---|---|
#5 | theming_field_html_twig-2265251-5.patch | 1.58 KB | danlinn |
#3 | theming_field_html_twig-2265251-3.patch | 773 bytes | danlinn |
Comments
Comment #1
danlinn CreditAttribution: danlinn commentedComment #2
danlinn CreditAttribution: danlinn commentedYep, it's a bug. I'm rolling a patch now.
Thanks,
Dan
Comment #3
danlinn CreditAttribution: danlinn commentedHere's the patch. The line that had fieldname + content type was missing.
Thanks,
Dan
Comment #4
jojonaloha CreditAttribution: jojonaloha commentedThe patch looks good.
Looking closer at
system_theme_suggestions_field()
though makes me think that https://api.drupal.org/api/drupal/core!modules!system!templates!field.ht... is out of date, since none of the entity-specific suggestions aren't listed in the documentation.Comment #5
danlinn CreditAttribution: danlinn commentedUploaded a new patch with updated documentation in field.html.twig.
Thanks,
Dan
Comment #6
jojonaloha CreditAttribution: jojonaloha commentedCool, looks good to me.
Comment #7
effulgentsia CreditAttribution: effulgentsia commentedI'm not sure about this. Will leave a more detailed comment later today.
Comment #8
effulgentsia CreditAttribution: effulgentsia commentedWhat's the use case for this? Bundle names are meaningful only in the context of a particular entity type. For example, if there's a node type named "article" and a taxonomy vocabulary named "article", those are different things. When would you want a node field and a taxonomy term field themed the same way solely because their bundle names happen to match?
Regardless of the above answer, this docs fix looks great, and we should at least get that in.
If we decide to leave system_theme_suggestions_field() alone because of my claim that a bundle name has no meaning outside the context of a specific entity type, then we should also remove this erroneous documentation.
Comment #9
swentel CreditAttribution: swentel commentedHmm interesting, that suggestion actually exists in D7 - see https://api.drupal.org/api/drupal/modules%21field%21field.module/functio...
I've been trying to figure out where we removed it, but can't find it immediately. In a way this could be seen as a regression, but if we find out why we removed it, we need to update the documentation.
$suggestions[] = 'field__' . $element['#entity_type'] . '__' . $element['#bundle'];
is an alternative though for original questionComment #10
swentel CreditAttribution: swentel commentedNote that we most likely removed this for collisions anyway and replaced it by fieldname_entitytype_bundle - still trying to find the exact issue, but this issue should just update the docs.
Comment #11
swentel CreditAttribution: swentel commented#1367354: [PP-1] The list of theme hook suggestions for "field" is incorrectly ordered and has namespacing collisions is a good reference, but it's not the issue where we changed it - If I find that one, we can close it though.
Comment #12
mgiffordThis still a concern in D8? Unassigned issue too.
Comment #24
smustgrave CreditAttribution: smustgrave at Mobomo commentedCurious if this is still an issue in D9/D10?
Comment #25
smustgrave CreditAttribution: smustgrave at Mobomo commentedClosing as outdated.
From what I can see in D9.5 is that fields already have a template suggestion with the content type in mind.