By artfulrobot


2012-03-30 16:56:07 8 Comments

I want to theme the search result view mode with a template.

I had it in my head that naming a template file node--article--search-result.tpl.php would do the trick, but I'm obviously wrong about that.

I realise I can do node--article.tpl.php and within this check $view_mode, but this is awkward with all the other view modes that I don't want to template.

Ideas?

3 comments

@Elijah Lynn 2014-09-04 22:37:11

Here is a function that allows you to add new ones dynamically. It also calls the corresponding preprocess function if you have one declared.

Then call drush cache-clear theme-registry to get it working.

To use it replace THEME with your theme's name, and place it in your themes template.php file.

e.g. For a theme named Droid you would call it droid_preprocess_node(&$variables, $hook) {...

function THEME_preprocess_node(&$variables, $hook) {
  $view_mode = $variables['view_mode'];
  $content_type = $variables['type'];
  $variables['theme_hook_suggestions'][] = 'node__' . $view_mode;
  $variables['theme_hook_suggestions'][] = 'node__' . $view_mode . '_' . $content_type;

  $view_mode_preprocess = 'THEME_preprocess_node_' . $view_mode . '_' . $content_type;
  if (function_exists($view_mode_preprocess)) {
    $view_mode_preprocess($variables, $hook);
  }

  $view_mode_preprocess = 'THEME_preprocess_node_' . $view_mode;
  if (function_exists($view_mode_preprocess)) {
    $view_mode_preprocess($variables, $hook);
  }
}

@Bas 2014-08-27 13:15:06

The Entity view modes module adds these template suggestions automatically

The Drupal 7 successor to Build modes which will allow administrators to define custom view modes for entities. Custom entities are added to the entity registry via hook_entity_info_alter() so they are available to any code that uses entity_get_info() to provide a list of view modes for an entity. This includes node and user reference fields, Views, etc.

@Clive 2012-03-30 17:04:41

Adding theme hook suggestions in a node preprocess function should do the trick:

function MYMODULE_preprocess_node(&$vars) {
  if ($vars['node']->type == 'article' && $vars['view_mode'] == 'search_result') {
    $vars['theme_hook_suggestions'][] = 'node__article__search_result';
  }
}

After clearing the caches you should be able to use node--article--search-result.tpl.php for your template file name.

NB. You could also do this in your theme's template.php file by calling the function MYTHEME_preprocess_node() instead.

@artfulrobot 2012-03-30 17:09:40

Brilliant! Thank you. Was just about to add a link to same advice here: mearra.com/blogs/juha-niemi/drupal-7-custom-node-view-modes

@Clive 2012-03-30 17:11:29

No problem :) Just FYI, search_result is already declared as a view mode so you don't need to implement hook_entity_info_alter() in your case

Related Questions

Sponsored Content

1 Answered Questions

How to override template for view mode of a entity within a module?

  • 2014-05-26 11:47:14
  • Codium
  • 530 View
  • 1 Score
  • 1 Answer
  • Tags:   7 theming entities

1 Answered Questions

How to change the View Mode based on theme?

  • 2012-08-27 15:52:22
  • PoeticNinja
  • 791 View
  • 3 Score
  • 1 Answer
  • Tags:   theming entities

1 Answered Questions

Preview template for node with custom template?

  • 2018-08-07 19:23:37
  • JOakland
  • 218 View
  • 1 Score
  • 1 Answer
  • Tags:   8 theming

1 Answered Questions

[SOLVED] How to check for view mode in twig template?

  • 2018-04-25 19:21:45
  • usernameabc
  • 1542 View
  • 1 Score
  • 1 Answer
  • Tags:   8 theming hooks

1 Answered Questions

[SOLVED] How to hide node title for specific content type using template file

  • 2016-08-25 10:44:51
  • pierostz
  • 277 View
  • 0 Score
  • 1 Answer
  • Tags:   7 theming

1 Answered Questions

[SOLVED] Custom view mode issues

2 Answered Questions

Custom template file for custom view mode on site front page

  • 2015-03-25 21:01:29
  • Muhammad
  • 88 View
  • 1 Score
  • 2 Answer
  • Tags:   theming nodes

0 Answered Questions

Drupal 7: Multiple View Mode in Views Search Filter

1 Answered Questions

[SOLVED] Custom tpl for view mode for all contenty type

  • 2013-01-23 19:16:00
  • arrubiu
  • 168 View
  • 3 Score
  • 1 Answer
  • Tags:   7 theming entities

2 Answered Questions

[SOLVED] How to add more Display Suite template suggestions

Sponsored Content