Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
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
Comment #1
fmizzell CreditAttribution: fmizzell commentedyou 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:
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.
Comment #2
Toxid CreditAttribution: Toxid commentedWonderful! 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.
Comment #3
fmizzell CreditAttribution: fmizzell commentedComment #5
fmizzell CreditAttribution: fmizzell commentedHey 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.
Comment #6
Toxid CreditAttribution: Toxid commentedIt'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.
Comment #7
JordanMagnuson CreditAttribution: JordanMagnuson commentedI 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.
Comment #8
fmizzell CreditAttribution: fmizzell commented@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.
Comment #9
JordanMagnuson CreditAttribution: JordanMagnuson commentedThis 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):
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.
Comment #10
crispin CreditAttribution: crispin commentedJust 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
Comment #11
subhojit777@fmizzell suggestion at #1 works
Comment #12
lucas.constantino CreditAttribution: lucas.constantino at Taller commentedI 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