Problem/Motivation

#1367354: The list of theme hook suggestions for "field" is incorrectly ordered and has namespacing collisions is a pre-existing issue with field template suggestions.

#2808481: Introduce generic entity template with standard preprocess and theme suggestions is a new issue adding generic entity template suggestions that could run into the same issue.

We need to figure out whether to namespace suggestions (i.e. bundle-$suggestion or b-$suggestion) and which format to use to prevent the potential for conflicts. This would also allow the targeting of the template to be figured out from the filename rather than just the logic in the theme registry building.

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Comments

catch created an issue. See original summary.

berdir’s picture

Cross-posting from #2808481-62: Introduce generic entity template with standard preprocess and theme suggestions

So.. how do we get to the point of making a descision here? I think we have all the options laid out:

a) No prefixes, like on existing theme suggestions => status quo, with all the advantages (shorter) and disadvantages (not self-explaining, possible conflicts)
b) Full prefixes, bundle-, view-mode-, .. (longer but somewhat self-explaining and no risk of conflicts)
c) shortened prefixes, b-, v-, (shorter, not really self-explaining anymore unless you know the abbreviations, no conflicts)

Personally I'm fine with a or b, I really don't care. Not sure if c) isn't actually harder to understand than a) and whether it is worth doing that just to avoid conflicts.

tstoeckler’s picture

So the beef I have with b) or c) (or in other words with the reasoning behind this issue) - regardless of the actual experience for themers - is that it would be introducing a one-off for entity templates. There are, however, numerous template suggestions which can conflict, and which currently suffer from the same issue. So not only would we be introducing an inconcistency between current entity templates (node, user, ...) and the new generic entity template - which I think is acceptable as the former can be seen as the deprecated BC-layer of the latter - but we would be introducing an inconcistency between the generic entity template and every other template.

Other conflicts we currently have:

  • block__$provider / block__$base_plugin_id / block__$derivative_plugin_id / block__$id
  • field__$field_type / field__$field_name
  • field__$entity_type_id__$bundle / field__$entity_type_id__$field_name

And this is just in core. I didn't check but I am sure that there are more in the world of other uber-generic modules like Panels / Display Suite / Layout Plugin / Search API / ...

fabianx’s picture

However those should be changed by adding new suggestions for those as well and deprecate the old suggestions.

The theming community in #twig (slack) overall (though it was a non-representative poll) was for long and descriptive names by a large margin.

Only a few for the shortened names and only another few for keeping the old convention with conflicts.

catch’s picture

The issue title already mentions field templates - I think we should come up with a pattern here and apply it elsewhere as Fabianx says.

markhalliwell’s picture

The theming community in #twig (slack) overall (though it was a non-representative poll) was for long and descriptive names by a large margin.

This was only asked once and getting a few people's opinion who were in that room really doesn't indicate a "large margin".

---

That being said, I am in favor of explicitly long suggestions, only if template discovery was altered to also allow these suggestions to be broken up into folders (as was semi-depicted in #2808481-56: Introduce generic entity template with standard preprocess and theme suggestions).

---

After thinking about the above (folder) comment a bit more, I think the easiest solution would be to allow a 1:1 mapping of __ to a directory separator (/):

node__bundle_foo__view_mode_bar__1234

Would become:

templates/node/bundle-foo/view-mode-bar/1234.html.twig

And, if not found, would search for the original long ass filename (for BC and themers who "like" these.......):

templates/node--bundle-foo--view-mode-bar--1234.html.twig

This gives the ability to dramatically reduce the length of a filename from node--bundle-foo--view-mode-bar--1234.html.twig to simply 1234.html.twig.

Moving forward, I think simply adding directory detection is a good enough compromise to support these ever growing theme hook suggestions.

rainbowarray’s picture

I would definitely like to avoid using directories as a way to shorten file names. Being able to place templates in directories of my choosing is important to me when working on a theme.

My preferences would be as follows:

Full prefixes but long file names > Abbreviated prefixes and slightly longer file names > Current system where conflicts happen because of lack of prefixes > Directory structure required for template suggestions

markhalliwell’s picture

Being able to place templates in directories of my choosing is important to me when working on a theme.

Why? This sounds more like personal preference than a logical dispute. We get it, you don't like directories or rather you like naming them arbitrarily. That's fine.

My suggestion above still allows filenames to be 10 miles long if you really want them to be and place them wherever you want.

If this kind of directory support isn't included, however, then y'all are forcing us to use these long winded names.

That's unacceptable.

This simply gives very complex themes (that have many, many templates) the ability to have a folder structure that makes sense.

It's very similar to PHP's PSR autoloading, if you will.

rainbowarray’s picture

I wouldn't mind if directories are an option if people want to make use of that. If I can have long file names and others can have short file names with directories, that would be cool.

Organizing templates by component is useful. Often it takes multiple templates of different types to get all the templates for a particular component.

SlayJay’s picture

as much as I dislike the folder idea, it is consistent with the rest of drupal 8.

If I want to make a new entity type I have to put it in an Entity folder in my module.

Having to put entity templates in an Entity folder in my module or theme seems like the most consistent / least confusing method.

It's a pain in the ass, especially if you're used to putting theme files in your own folder structures (I am)... but it was also a pain in the ass for module developers to have to start putting files into folders as well, and we got over it :P

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

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now 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.

manuel garcia’s picture

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

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now 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.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now 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.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now 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.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.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.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). 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.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now 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.

krzysztof domański’s picture

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

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

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

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

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

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

darvanen’s picture

Triaged as part of the Bug Smash Initiative.

Closing as duplicate of #1367354: The list of theme hook suggestions for "field" is incorrectly ordered and has namespacing collisions.

If that is wrong, please reopen by setting to 'Active' and add a comment explaining why.

berdir’s picture

Title: Bundle / field name / view mode theme suggestions can conflict » [policy] Bundle / field name / view mode theme suggestions can conflict
Version: 9.4.x-dev » 11.x-dev
Category: Task » Plan
Status: Closed (duplicate) » Active

4 years later...

I think this is not a duplicate, but the meta issue of that or maybe better, a policy issue on whether or not we should do prefixes, it also references the entity issue that was essentially blocked on this.

I did make a specific proposal with a few questions in #2752443: Incorrect order and duplicate theme hook suggestions and would appreciate feedback there, but I think this is still an issue that should have its own resolution on whether or not we use prefixes or not, and if all entries should use prefixes or only less common ones, likely guided by the specific examples in the field issue.

catch’s picture

#3566261: [regression] custom field twig for second comment fields has null value in comment after 11.3.0 upgrade just ran into this. We could use an issue summary update here.

@berdir I can't see a comment from you on #2752443: Incorrect order and duplicate theme hook suggestions, should that be a different issue link?

berdir’s picture

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.