I need to theming a cck field for particular content type.

Field: field_photo
Content type: events

In API field.html.twig

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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

danlinn’s picture

Assigned: Unassigned » danlinn
danlinn’s picture

Yep, it's a bug. I'm rolling a patch now.

Thanks,
Dan

danlinn’s picture

Status: Active » Needs review
FileSize
773 bytes

Here's the patch. The line that had fieldname + content type was missing.

Thanks,
Dan

jojonaloha’s picture

The 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.

danlinn’s picture

Uploaded a new patch with updated documentation in field.html.twig.

Thanks,
Dan

jojonaloha’s picture

Version: 8.0-alpha11 » 8.x-dev
Status: Needs review » Reviewed & tested by the community

Cool, looks good to me.

effulgentsia’s picture

Assigned: danlinn » effulgentsia
Status: Reviewed & tested by the community » Needs review

I'm not sure about this. Will leave a more detailed comment later today.

effulgentsia’s picture

  1. +++ b/core/modules/system/system.module
    @@ -706,6 +706,7 @@ function system_theme_suggestions_field(array $variables) {
    +  $suggestions[] = 'field__' . $element['#field_name'] . '__' . $element['#bundle'];
    

    What'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?

  2. +++ b/core/modules/system/templates/field.html.twig
    @@ -8,8 +8,11 @@
    - * type, any of the following templates will override this default
    + * type of entity type 'node', any of the following templates will override this default
      * implementation. The first of these templates that exists is used:
    + * - field--node--body--article.html.twig
    + * - field--node--body.html.twig
    + * - field--node--article.html.twig
    

    Regardless of the above answer, this docs fix looks great, and we should at least get that in.

  3. +++ b/core/modules/system/templates/field.html.twig
    @@ -8,8 +8,11 @@
      * - field--body--article.html.twig
      * - field--article.html.twig
    

    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.

swentel’s picture

Hmm 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 question

swentel’s picture

Status: Needs review » Needs work

Note 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.

swentel’s picture

#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.

mgifford’s picture

Assigned: effulgentsia » Unassigned

This still a concern in D8? Unassigned issue too.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs work » Postponed (maintainer needs more info)

Curious if this is still an issue in D9/D10?

smustgrave’s picture

Status: Postponed (maintainer needs more info) » Closed (outdated)
Issue tags: +Bug Smash Initiative

Closing as outdated.

From what I can see in D9.5 is that fields already have a template suggestion with the content type in mind.