Related Issues
Original core bug report: #939462: Specific preprocess functions for theme hook suggestions are not invoked
As part of the Drupal.org D7 Upgrade initiative, the Bluecheese Team has added a temporary fix to template.php in order to mitigate any potential bugs that might arise. See #1822338: Support theme template suggestions in entity module
Problem/Motivation
// Add suggestions.
$variables['theme_hook_suggestions'][] = $entity_type;
$variables['theme_hook_suggestions'][] = $entity_type . '__' . $bundle;
$variables['theme_hook_suggestions'][] = $entity_type . '__' . $bundle . '__' . $variables['view_mode'];
if ($id = entity_id($entity_type, $entity)) {
$variables['theme_hook_suggestions'][] = $entity_type . '__' . $id;
}
Those template suggestions are not working as by default the theme system only picks up suggestions that match the pattern of $base_hook . '__'.
Attached patch should fix that by specifying a custom pattern used to look for templates.
Additionally, I guess we should fix template suggestions to do not include the bundle if the entity type does not make use of bundles.
Comment | File | Size | Author |
---|---|---|---|
#23 | d7_entity_template_fix.patch | 860 bytes | fago |
#1 | entity_templates.patch | 928 bytes | fago |
Comments
Comment #1
fagoand here the mentioned patch...
Comment #2
mradcliffeI'm pretty sure that core theme ignores pattern completely and is hardcoded to only use
__
.Comment #3
tim.plunkettIs this related to #939462: Specific preprocess functions for theme hook suggestions are not invoked? Seems similar.
Comment #4
tim.plunkettI'm able to set up a
theme_ENTITY_TYPE()
, but no matter what I do,template_preprocess_ENTITY_TYPE()
won't run. Not sure if that's a separate issue, but the above patch doesn't resolve that.Comment #5
mradcliffeI am pretty sure, with that patch, you still would only have template_preprocess_entity() since core ignores pattern.
Comment #6
fagoPattern isn't ignored, the patch works. Still, as noted in #4 only template_preprocess_entity() works as the the template system doesn't call template preprocessors on inherited templates :/ So that's the next problem we have :/ Maybe, we should try to figure out the invoked template in template_preprocess_entity() and just invoke preprocessor for that template manually there?
Comment #7
mradcliffeYou're right, sorry.
Comment #8
barraponto CreditAttribution: barraponto commented@fago: I'm trying to get #939462: Specific preprocess functions for theme hook suggestions are not invoked commited since it would save us a lot of work. And, honestly, calling the template functions ourselves might be overkill once that patch goes in.
Comment #9
geaseStill, the patch #1 doesn't work for template suggestions like entity_name.tpl.php. They are still listed in template_preprocess_entity() and listed by theme developer, which is misleading.
Comment #10
freddybushboy CreditAttribution: freddybushboy commentedI had a similar problem which I managed to fix by adding this to my template.php:
Seems the templates just needed 'entity' in front of them..
Comment #11
fubhy CreditAttribution: fubhy commentedI don't think that is a good idea. This has to be fixed in core and by adding a custom workaround for entity templates only we would just make it more confusing.
Let's try to push #939462: Specific preprocess functions for theme hook suggestions are not invoked instead and get it commited to D7 as well.
I sometimes add a custom workaround for that in my themes by using hook_theme_registry_alter to manually inject these specific theme hooks into the registry. This is the most generic (and only global) workaround I was able to come up with. However, adding that functionality just for one single module's templates would be really confusing, especially for beginners ("Why does this work for my entity preprocess hooks but not for anything else?").
Comment #12
andyhu CreditAttribution: andyhu commented#10 works for me, thanks for sharing
Comment #12.0
andyhu CreditAttribution: andyhu commentedUpdated issue summary.
Comment #12.1
Senpai CreditAttribution: Senpai commentedAs part of the Drupal.org D7 Upgrade initiative, the Bluecheese Team has added a temporary fix to template.php in order to mitigate any potential bugs that might arise. See #1822338: Support theme template suggestions in entity module
Comment #13
adamdicarlo CreditAttribution: adamdicarlo commentedI take it it's not really possible to fix this bug in Entity API module, without the bug in Drupal 7 core being fixed?
If that's the case, then it's possible that Entity API is blocked from a stable release for a looonnng time. Is it really possible that #939462: Specific preprocess functions for theme hook suggestions are not invoked will get fixed in Drupal 7 core anytime soon?
An Entity API stable release is currently listed as a blocker for the Drupal.org D7 upgrade, so we're trying to figure out what to do about this....
Comment #14
kscheirerWell, the core fix would be nice, but that will have to go into D8 first, and then get backported to D7, so it might take a while to get that.
#939462: Specific preprocess functions for theme hook suggestions are not invoked has a test, but no patch yet.
#1751194: Introduce hook_theme_suggestions[_HOOK]() and hook_theme_suggestions[_HOOK]_alter() has a patch, seems like the best place to fix this problem.
Comment #15
Senpai CreditAttribution: Senpai commented@Adamdicarlo, here's the plan. I have commitment from the D7 and D8 core maintainers that if we create and review an RTBC patch for each version of core, we can get the core bug committed and thus unblock the stable release of Entity API and thus unblock the D7 upgrade.
I've been informed that @fabianx is the person to ping about the best method and/or approach to fixing this core bug, and perhaps @kscheirer and @grendzy should be in on that discussion?
Comment #16
mitchell CreditAttribution: mitchell commented#15: Ok. So this is definitely a duplicate now.
See #939462: Specific preprocess functions for theme hook suggestions are not invoked.
Comment #17
fagoyep, it's a duplicate but let's keep it open to track this issue for the entity module and/or providing a work-a-round.
I don't think this will land in d7 anytime soon, so I'd be fine adding a interim fix. So what about the patch in #1?
Comment #18
fagoComment #19
fagook, I had another look at this and fixed it based upon the patch in #1. I improved the pattern a bit though, such that $entity_type.tpl.php templates also work. Also see the change notice for more infos on the impact of this change.
Comment #20
gmclelland CreditAttribution: gmclelland commented@fago For some reason when I tried the latest dev version in the git repo one of my views blocks that uses a custom tpl stops showing output.
I have a search_api_solr view where the filters are exposed as a block. I theme this block by overriding the block using views-exposed-form--site-search--page.tpl.php suggestion.
I have cleared the cache several times, but the only thing that seems to work is to revert back to the 1.0 release of the entity api module.
Comment #21
kscheirerSetting back to needs work for #20.
Comment #22
Anonymous (not verified) CreditAttribution: Anonymous commentedI stumbled across this. If there are no custom entity types defined (i.e. entity_crud_get_info() returns an empty array()), then the patterns ends up being a very gobbly \.|__ which effectively sets 'entity' as the base hook for all theme functions that contains two underscores (__). This causes all kinds of weird things to break!
Comment #23
fagoouch - I see. Here is an updated patch what fixes the problem for me. Please give it test-run, clear caches and report results.
Comment #24
gmclelland CreditAttribution: gmclelland commented@fago - Just tested... I'm not seeing the problem I was having #20 after applying the patch in #23.
Comment #25
fagothanks for testing, committed.
Comment #27
kevinquillen CreditAttribution: kevinquillen commentedIs this going to be rolled into a new release soon? This one gets me every time I go to override an entity template.
Comment #28
inventlogic CreditAttribution: inventlogic commentedwell this is unbelievable so what is the state of play?
shouldn't it just be for example:
custom-entity-name--custom-entity-bundle--view-mode.tpl.php
no theme_hook suggestions required.
does the above work now in dev?
Comment #29
fubhy CreditAttribution: fubhy commentedWhat you just described ARE theme hook suggestions o_O
Comment #30
inventlogic CreditAttribution: inventlogic commentedyes but you should not have to specifically declare it in a hook_theme() function.
right now i had to declare the template in a custom module using hook_theme() whereas if Drupal was working correctly it would be automatically included which it is not.
Comment #31
kevinquillen CreditAttribution: kevinquillen commentedWhat he is trying to say is that you should be able to just use the hook suggestions provided by EntityAPI, by adding the tpl file into your theme. Currently that does nothing without the patch above, or, injecting your own template suggestion via template.php.
So, EntityAPI provides entity_type--entity-bundle--view-mode suggestion, but creating that tpl file in your theme does absolutely nothing (right now). It is very confusing. I could not get it to respect this file at all without the patches above (#1 and #23).
Comment #32
inventlogic CreditAttribution: inventlogic commentedI see there is a module https://drupal.org/project/view_mode_templates that would seem to partly fix this issue for view modes on entities.
Anyone try it?
Comment #33
kevinquillen CreditAttribution: kevinquillen commentedThat isn't necessary - entity_view_modes creates the same template suggestions. The problem is EntityAPI is not recognizing the file patterns that entity_view_modes creates. The patches in #1 and #23 fix that.
Comment #34
inventlogic CreditAttribution: inventlogic commentedThanks. Got it now and both patches are in the latest 7.x-1.x-dev so updating to latest dev of entity API fixes this.
Comment #35
Paul B CreditAttribution: Paul B commentedIt seems this fix breaks the reply module if you override reply.tpl.php -- see http://drupal.org/node/1994298
Comment #36
drummI don't think this is affecting Drupal.org D7, untagging.
Comment #37
hughworm CreditAttribution: hughworm commentedJust a question: My fix was to patch entity.module to prefix all theme suggestions in template_preprocess_entity() with 'entity__' . That way theme discovery automatically recognises them as based on 'entity'. Wouldn't that be better that adding code to entity_theme() as per the patch #23?
Comment #38
Alexander Allen CreditAttribution: Alexander Allen commentedPaul B,
According to comments #20 and #23, issue #1994298 seems to have been fixed.
Comment #40
corbacho CreditAttribution: corbacho commentedI was using the trick of "preprocess_entity" suggested in #10 (and used in d.o #1822338: Support theme template suggestions in entity module )
But it has stopped working after upgrading entity to 1.2
If you were also using that, you need to rename the templates you were using. If you had:
entity__whatever...tpl.php
Just needs to be
whatever...tpl.php
Then you are fine.
I actually liked the previous way, prefixing templates with "entity", it's easier to recognise them.
Comment #40.0
corbacho CreditAttribution: corbacho commentedOriginal core bug report: #939462: Specific preprocess functions for theme hook suggestions are not invoked
Comment #41
heylookalive CreditAttribution: heylookalive commentedRe-opening as I'm experiencing this with current stable entity api.
I've defined a custom entity with custom templating equatable to node, so hook_theme, preprocessor and template etc.
This works fine until the entity template file is copied to a theme, after which the preprocessor fails to fire.
This is down to this related issue #2225491: entity_theme not calling custom templates.
The above code is where the pattern is set, and through hook_theme the "base hook" is set to "entity" for any entity api defined entities, my current workaround is to hook_theme_registry_alter and unset the "base hook" index in the registry for my custom entities.
Not sure what the best approach to fixing is but a possibility is an extra index for hook_entity_info where you can disable inclusion from
$types
.Comment #42
Paul B CreditAttribution: Paul B commentedReply is still having problems as well, see #2403805: Template preprocess variables are not available when reply.tpl.php is overridden in a theme.