0

I have a view / display page that needs to be displayed in a specific html - page format. I did a html--viewname.tpl.php and page--viewname.tpl.php to display my view.

Within page--viewname.tpl.php I call print render($page['content']); to render my view. All is working except that block.tpl.php and region.tpl.php are called by drupal system to wrap the view content adding several html tags (div) that I don't want.

I expected to use function MYTHEME_preprocess_block(&$variables) to ask drupal to use a custom block.tpl.php file, but within this function I didn't find the way to know if the function is called to render my viewn or anything else.

1 Answer 1

1

When dealing with a page view you can use the views_get_page_view() function, to get the view object for the current page, like this:

/**
 * Variable preprocessor for the block template.
 */
function MYTHEME_preprocess_block(&$variables) {
  // If we are on a views page display.
  if ($view = views_get_page_view()) {
    // If we are on the MYVIEW view.
    if ($view->name == 'MYVIEW') {
      // Add the view object to the variables for use in the template (if you need it).
      $variables['view'] = $view;

      // Use a custom block template for this specific display (if required).
      $variables['theme_hook_suggestions'][] =  "block__{$view->name}";

      // Add your block customisations for this specific view (if required).

      // If we are also on the MYDISPLAY display of MYVIEW.
      if ($view->current_display == 'MYDISPLAY') {
        // Add your block customisations for this specific view and display here (if required).

        // Use a custom block template for this specific display (if required).
        $variables['theme_hook_suggestions'][] =  "block__{$view->name}__{$view->current_display}";
      }
    }
  }
}
3
  • Thanks rooby, it is what I was looking for, now I don't understand why drupal is still calling the default block.tpl.php instead of my custom one set in the preprocess function. I'm still debugging to understand that...
    – Pierre_C
    Commented Dec 22, 2015 at 12:24
  • Ok, in fact when $variables['theme_hook_suggestions'][] = "block__XXX_YYY"; drupal looks for template file block--XXX-YYY.tpl.php. Strange because I did the same for html template using html--XXX--YYY as suggestion and html--XXX-YYY.tpl.php as template file name without any problem. I will ask a question about that.
    – Pierre_C
    Commented Dec 22, 2015 at 12:58
  • Underscores in template suggestions are converted to dashes in the template file name. Dashes are left as dashes.
    – rooby
    Commented Dec 22, 2015 at 23:45

Your Answer

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

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