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

<?php
 
// 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.

Files: 
CommentFileSizeAuthor
#23 d7_entity_template_fix.patch860 bytesfago
PASSED: [[SimpleTest]]: [MySQL] 374 pass(es).
[ View ]
#1 entity_templates.patch928 bytesfago
PASSED: [[SimpleTest]]: [MySQL] 353 pass(es).
[ View ]

Comments

Title:template suggestions are not workingtemplate suggestions are not working if no custom template is defined
Status:Active» Needs review
StatusFileSize
new928 bytes
PASSED: [[SimpleTest]]: [MySQL] 353 pass(es).
[ View ]

and here the mentioned patch...

I'm pretty sure that core theme ignores pattern completely and is hardcoded to only use __.

I'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.

I am pretty sure, with that patch, you still would only have template_preprocess_entity() since core ignores pattern.

Status:Needs review» Needs work

I am pretty sure, with that patch, you still would only have template_preprocess_entity() since core ignores pattern.

Pattern 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?

You're right, sorry.

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

Still, 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.

I had a similar problem which I managed to fix by adding this to my template.php:

function THEMENAME_preprocess_entity(&$variables) {
  foreach($variables['theme_hook_suggestions'] as &$suggestion) {
    $suggestion = 'entity__' . $suggestion;
  }
}

Seems the templates just needed 'entity' in front of them..

Maybe, we should try to figure out the invoked template in template_preprocess_entity() and just invoke preprocessor for that template manually there?

I 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?").

#10 works for me, thanks for sharing

Issue summary:View changes

Updated issue summary.

Issue summary:View changes

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

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

Well, 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.

Priority:Major» Critical
Issue tags:+Drupal.org D7, +porting, +16hr

@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?

Status:Needs work» Closed (duplicate)

#15: Ok. So this is definitely a duplicate now.

See #939462: Specific preprocess functions for theme hook suggestions are not invoked.

Status:Closed (duplicate)» Active
Issue tags:-D7 stable release blocker

yep, 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.

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

Status:Active» Needs review

Status:Needs review» Fixed

ok, 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.

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

Status:Fixed» Needs work

Setting back to needs work for #20.

I 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!

Status:Needs work» Needs review
StatusFileSize
new860 bytes
PASSED: [[SimpleTest]]: [MySQL] 374 pass(es).
[ View ]

ouch - I see. Here is an updated patch what fixes the problem for me. Please give it test-run, clear caches and report results.

@fago - Just tested... I'm not seeing the problem I was having #20 after applying the patch in #23.

Status:Needs review» Fixed

thanks for testing, committed.

Status:Fixed» Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Is this going to be rolled into a new release soon? This one gets me every time I go to override an entity template.

well 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?

custom-entity-name--custom-entity-bundle--view-mode.tpl.php

no theme_hook suggestions required.

What you just described ARE theme hook suggestions o_O

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

What 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).

I 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?

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

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

Status:Closed (fixed)» Active

It seems this fix breaks the reply module if you override reply.tpl.php -- see http://drupal.org/node/1994298

Issue tags:-Drupal.org D7, -porting, -16hr

I don't think this is affecting Drupal.org D7, untagging.

Just 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?

Status:Active» Fixed

Paul B,

According to comments #20 and #23, issue #1994298 seems to have been fixed.

Status:Fixed» Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

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