4

Short version

My aim is to theme a file field in a view of node fields. In the view configuration, the field formatter is set to Rendered file and a view mode is selected. How do I suggest a template file?

Long version

What I see is, theming of a file entity is done through file_entity.tpl.php in file_entity module. I copied this file to my theme folder and tried following names:

file_entity--my-view-mode.tpl.php
file-entity--my-view-mode.tpl.php
file_entity--views--my-view-mode.tpl.php
file-entity--views--my-view-mode.tpl.php

None worked. So I tried to hook in some suggestions like below as view_mode_templates.module also does.

<?php
MYTHEME_preprocess(&$vars){
  $vars['theme_hook_suggestions'][] = 'file_entity__views__my_view_mode';
}
?>

This ends up loading that file-entity--views--my-view-mode.tpl.php but funnily I get an error like this:

Fatal error: Only variables can be passed by reference in blah/blah/file-entity--views--my-view-mode.tpl.php on line 71

Which is actually this line:

hide($content['links']);

Funnier thing is if I comment that line out, I get a fatality masterkill WSOD with no error message.

If I try to do it like below, again as view_mode_templates.module does, it doesn't see the suggestion.

<?php
function MYTHEME_preprocess(&$vars) {
  if (array_key_exists('file_entity', entity_get_info())) {
    $vars['theme_hook_suggestions'][] = 'file_entity__views__my_view_mode';
  }
}
?>

So, does it maybe not suggest the theme suggestion in the correct place? Do I need to suggest this in something like template_preprocess_file_entity(&$variables) or template_preprocess_entity(&$variables)? Which don't have the appropriate $vars['theme_hook_suggestions'] array as far as I couldn't see.

Any comment is hardcore appreciated, thanks.

8
  • 1.Are you trying to change the template of file-field (from Field module) or FileEntity default template (from File Entity module)? 2.If you are trying to override the default template of FileEntity, then did you clear the cache after you've copied it?
    – xurshid29
    Dec 25, 2013 at 14:06
  • U need to follow drupal.stackexchange.com/questions/95747/… and for suggestions use the devel themer module.
    – madhurjya
    Dec 25, 2013 at 14:13
  • 1. @xurshid29 I am trying to change the FileEntity default template from File Entity module. Going on with Filed module doesn't look possible as per Issue: File entity views fields outputting as a link are broken. They are using the view modes for the file entities. 2. Yes I did, on each trial, more than twice.
    – duru
    Dec 25, 2013 at 15:00
  • @madhurjya thank you, but since what I am trying to theme is not a node but a file entity, can you please be a little more specific on the implementation?
    – duru
    Dec 25, 2013 at 15:04
  • above, are you actually calling MYTHEME_preprocess() or MYTHEME_preprocess_file_entity()?
    – Jimajamma
    Dec 25, 2013 at 15:19

2 Answers 2

6

There's a documentation about template suggestions at Displaying Media under the heading Further Theming.

The theme suggestions provided by the File Entity module are:

file__{file type}
file__{file type}__{view mode}
file__{mime type}
file__{mime type}__{view mode}
file__{file id}
file__{file id}__{view mode}
5
  • God, I read that docs more than 10 times in the summer and now forgot what they were about. Thank you very much Елин Й.
    – duru
    Dec 25, 2013 at 17:27
  • The above listed formats are missing some underscores due to some editors issue, I couldn't fix. Please refer to the docs: drupal.org/node/1793548
    – duru
    Dec 25, 2013 at 17:30
  • I've corrected them changing the formatting from block quote to code block. Dec 25, 2013 at 17:35
  • You're welcome. I'm glad that I could help you to solve your problem. Dec 25, 2013 at 20:17
  • omg, after a year i was looking for the same thing and i googled it and came upon this, read it without knowing that it was mine. so funny:)
    – duru
    Oct 9, 2014 at 16:47
0

Here's an actual example for "document" file type:

file--document.tpl.php
file--document--default.tpl.php   // default view
file--application-pdf.tpl.php   // pdf mime type
file--application-pdf--default.tpl.php
file--1234.tpl.php   // file id
file--1234--default.tpl.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.