I've created an entity with eck that's called "post". I'm trying to override the entity.tpl.php. Theme developer module suggest this name: post--post.tpl.php. But the entity.tpl.php is still used, not the new one. If I create a page--post--post.tpl.php, then the page get's overridden as it should. But as I said earlier, it's the entity.tpl.php that I want to override. Any tips?

Comments

fmizzell’s picture

you are on the right track, the entity API offer us a few theme suggestions that we can use to override how our entities look.
Let's say that you entity type is called "post" and one of your bunbles is called "blog", and that you have created an entity with id 1. The following theme suggestions are available:

post.tpl.php
post__blog.tpl.php
post__blog__teaser.tpl.php (teaser is a view mode)
post__1.tpl.php

So you can create any of those and they should work, but I believe that for those template files to be pick up you need to let drupal know through the theme hook. You can call this hook in the .module file of a custom module or in your template.php file of you theme.

So if you want to use the post__blog.tpl.php you can call the theme hook like this:

function MYMODULE_theme(){
  $theme = array();

  $theme['post__blog'] = 
  array(
    'render element' => 'elements',
    'template' => 'post__blog'
  );

  return $theme;
}

after you set up your theme hook make sure to clear the cache, and also make sure that there is something in your tpl.php file, if it is empty it will still be ignored.

I hope this helps.

Toxid’s picture

Wonderful! I've examined several theme hooks but I never thought of just hook_theme. Thanks alot! Perhaps it would be a good idea to include this in the readme.txt file? Or perhaps it belongs to entity APIs readme. In my opinion it's relevant documentation for both entity API and ECK.

fmizzell’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

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

fmizzell’s picture

Hey Toxid, I just found some new information.. I have not tried it myself, but it comes from a good source. I don't believe is necessary for you to declare your tpl.php in the hook_theme as I described above. The reason why Drupal is not picking up our templates is because we are not following the naming conventions.. Try naming your templates with - instead of _, and Drupal should pick it up automatically. So in the example I gave above if the template is called blog--post.tpl.php, it should get picked up by Drupal without having to do anything in hook_theme. I am planning to try this out myself.. but if you get to it before me, let us know.

Toxid’s picture

It's a good thought, but it seems like _ or - doesn't matter. When I declare the file in the theme function, then both _ and - is picked up. If I don't declare the file in my themefunction, none of the names are picked up. It would be nice if someone else can confirm this behaviour, in case there's something wrong with my installation. I'm using the default Bartik theme.

JordanMagnuson’s picture

I know this is an old topic... but just wanted to confirm that I am getting the same behavior. Drupal is not automatically picking up my entity template files, unless I declare them specifically in the theme function, as per above.

fmizzell’s picture

@JordanMagnus, yes I believe that is the only way to override the entity.tpl.php, or to use the hook suggestions provided by the entity api.

JordanMagnuson’s picture

This seems to be a bug in the entity module: http://drupal.org/node/1462772

Not sure which way is the best to get around it, but I've opted for the method mentioned there, rather than using hook theme (prepending "entity--" to all template suggestions):

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

Then creating a template file named entity--my-entity.tpl.php

With this solution you don't have to go back and add each entity theme file individually to hook_theme.

crispin’s picture

Issue summary: View changes

Just in case anyone comes to this old thread - no need for the functions or anything.

The template suggestion naming convention is (as fmizzell said)

[my-entity-type]--[my-bundle].tpl.php

subhojit777’s picture

@fmizzell suggestion at #1 works

lucas.constantino’s picture

I think this has to do with a Drupal's core designed pattern: no module can override another module's theme. Basically, ECK is the module declaring your entities and your themes. Implementing the hook_theme is a way to override the theme definition, which indeed works.

More here: https://www.drupal.org/node/839376