2

I would like to define 'theme_hook_suggestions' for a custom views-view.tpl.php file.

To achieve this, I wrote the following code into my template.php file:

/**
 * Implements hook_preprocess_views_view().
 */
function MYTHEME_preprocess_views_view(&$variables) {
  // Some logic to decide if the hook_suggestions should set or not
  // ...
  $variables['theme_hook_suggestions'][] = 'foo';
}

After that, I created a copy of views-view.tpl.php in the templates folder named foo.tpl.php.

Sadly, my template file is ignored by Drupal and the default views-view.tpl.php file is rendered on the page.

What's the matter? I debugged the theme_registry and the template path should be correct (points to my theme).

Update The target is that I can switch between two template files which are defined in my preprocess function (see above). Because in some cases, my layout markup is significant different as in other cases.

I don't want to use the same template file for both cases; when I do this, the file is full of tousands of if() just for adjusting the markup for some cases.

6
  • Try to clear cache and check again.
    – Suresh R
    Sep 24, 2015 at 12:07
  • Clear the cache didn't solve the problem
    – moertle
    Sep 24, 2015 at 12:08
  • Why don't you use 'views-view.tpl.php' and customize it as per your requirement? Templates are generally used for customization.
    – Suresh R
    Sep 24, 2015 at 12:14
  • 1
    My suggestion is to check all conditions in the preprocess function and just do the rendering in template (which is actually the preferred way)
    – Suresh R
    Sep 24, 2015 at 12:25
  • 2
    I disagree with that @SureshR - taking a modular approach is much better than having a single template file with tons of ifs in it, IMO.
    – Clive
    Sep 24, 2015 at 12:49

2 Answers 2

3

Your approach is fine. The only problem is with the name of the hook suggestion and the equivelant tpl.php file.

Since you are preprocessing views_view the hook suggestion should be named something that starts with views_view ex: views_view__[anything]. Note that all the undersores will be replaced with dashes for the filename. So it should be something like views-view--[anything].tpl.php

For example:

/**
 * Implements hook_preprocess_views_view().
 */
function MYTHEME_preprocess_views_view(&$variables) {
  // Some logic to decide if the hook_suggestions should set or not
  // ...
  $variables['theme_hook_suggestions'][] = 'views_view__foo';
}

And the filename will be views-view--foo.tpl.php

1
  • That clarifies an issue I was facing.. Thank you..
    – W.M.
    Nov 12, 2016 at 16:44
0

You can have multiple templates for views like :

views-view--home-slideshow-mobile.tpl.php if your display is a block, you can write it like this :

views-view--home-slideshow-mobile--block.tpl.php then you have templates for views fields :

views-view-fields--home-slideshow-mobile.tpl.php Templates for views format (like unformatted for example) :

views-view-unformatted--home-slideshow-mobile.tpl.php If you want to alter the template of your block, you can declare a block.tpl.php like :

block--my-block-module--my-block-machinename.tpl.php

You can copy the template and paste them in your theme and rename it to target your specific view. form sites/all/contrib/views/theme/...

See this links really help you out: http://www.wdtutorials.com/how-to-create-custom-template-view-drupal-tutorial-article/7#.VgQw5XU4bK4

https://www.ostraining.com/blog/drupal/views-templates/

Best of Luck :)

1
  • Thanks for the extensively answer, but this is not the case. I like to switch the template file in my preprocess function.
    – moertle
    Sep 25, 2015 at 6:48

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.