Hi,

I would like to request an implementation of some kind of theme_hook_suggestions for views templates or a hook for adding / altering custom template suggestions without the need to create a views style or row plugin, which is quite a journey.

I ran into the following problem:

I wanted to add a 'custom' template suggestion for my unformatted view for multiple views based on the current display class name, so I could easily re-use my templates, instead of duplicating the same template over and over for the same use. Where dislpay class is the common denominator.

I expected template_process_views_view() to add a suggestion through the default theme_hook_suggestions, nothing happened. So I searched on...

I could not get the views_theme_functions() from any views_pre_* hooks cause the function is directly returned, and I found no other way then invoking it by extending it from a plugin class by theme_functions in the views_plugin class.

/theme/theme.inc: _views_theme_functions($hook, $view, $display = NULL)

The function which creates the suggestion has the hook, view object and display, perfect when you would like to add a suggestion based on display (style) class name.

The display is available and the view object, if there was any hook for this function, a suggestion could be added, without the need to create a full blown row or style plugin.

Does this fits or is there another way to simply add a template suggestion based on a view display property as a class name or a field property?

Thanks. Cheers,

Comments

ericclaeren’s picture

Status: Active » Needs review

Hi,

Have done some work for this feature, only thing needed is:

To change:

/**
 * Build a list of theme function names for use most everywhere.
 */
function views_theme_functions($hook, $view, $display = NULL) {
  require_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'views') . "/theme/theme.inc";
  return_views_theme_functions($hook, $view, $display); 
}

To

/**
 * Build a list of theme function names for use most everywhere.
 */
function views_theme_functions($hook, $view, $display = NULL) {
  require_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'views') . "/theme/theme.inc";
  $themes = _views_theme_functions($hook, $view, $display); 
  $context = array(
    'hook' => $hook,
    'view' => $view,
    'display' => $display,
  );  
  // will call all modules implementing hook_views_theme_functions_alter
  drupal_alter('views_theme_functions', $themes, $context);  
  return $themes;
}

With this change, it's possible to add template suggestions from one single function, I have made a display extender which adds an additional field to each display, where an identifier can be added, when there's no identifier the function below also adds template suggestions based on the display class(es). Real handy when you want to use 1 template for multiple displays and gives you more freedom.


/**
 * Implements hook_views_theme_functions_alter()
 */
function views_template_suggestion_views_theme_functions_alter(&$data, $context) {  
  $suggestions = array();
  
  // create template suggestion based on class name
  if ($view = $context['view']) {
    
    // empty
    $identifier = false;
    
    if ($view->display_handler->get_option('template_identifier')) {
      $identifier = $view->display_handler->get_option('template_identifier');
    } elseif ($view->display_handler->get_option('css_class')) {
      $identifier = $view->display_handler->get_option('css_class');
    }
    
    if ($identifier) {
      // replace unwanted chars from class
      $search =  array('_', '-');
      $identifier = str_replace($search, '__', $identifier);    
      $classes = explode(' ', $identifier);

      // build suggestions
      for ($i = 1; $i <= count($classes); $i++) {      
        $name = implode('__', array_slice($classes, 0, $i));
        // create template suggestions and add them at the beginning (reverse)   
        $suggestions[] = $context['hook'] . '__' . $name;
      }
      krsort($suggestions); // sort it small > large
    }
  }
 
  // add suggestions to beginning of data
  $data = $suggestions + $data;
}

I really would appreciate you opinion. This simple hook, will make it more easier to add a suggestion based on anything within the view.

dawehner’s picture

In general this kind of stuff shouldn't be done on runtime, because depending on the level it could quite some overhead.
For example if you have a complex function which is executed foreach rendered field this would be an issue.

My suggestion would be to use hook_theme_registry_alter and add your stuff in a custom preprocess function.

rudiedirkx’s picture

@dawehner Altering the theme registry isn't enough. I think what dreamlabs is trying to do is add a theme hook suggestion so that another template (not one of the defaults) is used. The preprosessing moment already exists via views_view_list or views_view_fields etc, and you can add theme_hook_suggestions there, but they're completely ignored: the same template is used and the new preprocessor isn't executed.

Maybe I'm completely missing the point, but some kind of template/theme hook alter would be good.

Currently, to have several views share the same custom template, you have to make your own (row/field) style plugin and add a theme hook suggestion there. That's weird.

rudiedirkx’s picture

I've found a way to do this that is acceptable IMO:

Use template_preprocess_views_view_list (or other) to set $variables['theme_hook_suggestion'] to an existing theme function + __specific_name. (Is a string, not array and doesn't exist by default.)

I've created 3 views ("short_terms", "short_users" and "short_nodes") and 1 custom template to show those (as short as possible): views-view-list--short-users.tpl.php. Obviously (without preprocessing) the template is only used by view "short_users". If I preprocess and set theme_hook_suggestion to "views_view_list__short_users", all 3 views use that template.

ericclaeren’s picture

Thanks guys,

By the way, I'm no die hard coder, so you may expect me to say something stupid. ;)

I made this, so a user in views can see the added suggestions, and it's not only a behind the scenes dev alteration. A hook_theme_registry_alter seems as dirty to me as my solution, although it's cached you're actually fooling views. And is not transparent.

Still believe that a mechanism for adding suggestions would be helpful, with a preference for the default use of theme_hook_suggestions. The current suggestions from (_views_theme_functions) are generated on every view request, maybe these suggestions with alterations could be cached when saving the view and emptied when editing a view. So these are not generated on every request but you still can see changes from your view in the template suggestion window.

Cheers,

dawehner’s picture

Why is bypassing the built in functionality for drupal less dirty then the actual recommended way to do it?

Maybe people get confused by the long hook name, but there are a lot of, for example themes, which often uses this hook.
Another point might be that the hook has a lot of arguments, which also scares them.

If you cache this information you always should think of "cache by what ...", simply per view/display might not always what you want

ericclaeren’s picture

Priority: Normal » Minor
Status: Needs review » Closed (won't fix)

I have written an new branch for this module: http://drupal.org/project/views_custom_template. Which adds a display option and a series of preprocess functions to the theme registry for custom templates and will be released (7.x-2.0) after it's reviewed by the module maintainer.