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.

CommentFileSizeAuthor
#23 d7_entity_template_fix.patch860 bytesfago
#1 entity_templates.patch928 bytesfago
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

fago’s picture

Title: template suggestions are not working » template suggestions are not working if no custom template is defined
Status: Active » Needs review
FileSize
928 bytes

and here the mentioned patch...

mradcliffe’s picture

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

tim.plunkett’s picture

tim.plunkett’s picture

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.

mradcliffe’s picture

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

fago’s picture

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?

mradcliffe’s picture

You're right, sorry.

barraponto’s picture

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

gease’s picture

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.

freddybushboy’s picture

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

fubhy’s picture

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

andyhu’s picture

#10 works for me, thanks for sharing

andyhu’s picture

Issue summary: View changes

Updated issue summary.

Senpai’s picture

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

adamdicarlo’s picture

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

kscheirer’s picture

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.

Senpai’s picture

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?

mitchell’s picture

Status: Needs work » Closed (duplicate)
fago’s picture

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?

fago’s picture

Status: Active » Needs review
fago’s picture

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.

gmclelland’s picture

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

kscheirer’s picture

Status: Fixed » Needs work

Setting back to needs work for #20.

Anonymous’s picture

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!

fago’s picture

Status: Needs work » Needs review
FileSize
860 bytes

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

gmclelland’s picture

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

fago’s picture

Status: Needs review » Fixed

thanks for testing, committed.

Status: Fixed » Closed (fixed)

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

kevinquillen’s picture

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

inventlogic’s picture

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?

fubhy’s picture

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

inventlogic’s picture

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.

kevinquillen’s picture

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

inventlogic’s picture

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?

kevinquillen’s picture

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.

inventlogic’s picture

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.

Paul B’s picture

Status: Closed (fixed) » Active

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

drumm’s picture

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

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

hughworm’s picture

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?

Alexander Allen’s picture

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.

corbacho’s picture

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.

corbacho’s picture

heylookalive’s picture

Issue summary: View changes
Status: Closed (fixed) » Active

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

/**
 * Implements hook_theme().
 */
function entity_theme() {
  // Build a pattern in the form of "(type1|type2|...)(\.|__)" such that all
  // templates starting with an entity type or named like the entity type
  // are found.
  // This has to match the template suggestions provided in
  // template_preprocess_entity().
  $types = array_keys(entity_crud_get_info());
  $pattern = '(' . implode('|', $types) . ')(\.|__)';

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.

Paul B’s picture