8

Since the case re-occurred to me for the second time recently, I decided to ask the question so to perhaps learn something.

In Views, the main display title (the one that usually gets rendered at the top of the view) is available in display output as $title but does not seem to be available in style output at all. Instead, the $title variable in style output means the name of a group if grouping is used.

What I want is to have that display title ('human name') available in any template as $display_title. So what I did was putting this code in my template.php:

function MYTHEME_preprocess_views_view(&$variables){
  if($variables['name'] == 'studios' && $variables['display_id'] == 'block'){
    $view = $variables['view'];
    $variables['display_title'] = $view->human_name;
  }
  dpm($variables);
}

...hoping to have the human title handy: <?php print $display_title; ?>

...but no joy. What am I doing wrong?

10

As you would also know, a view has several templates, which can vary depending on the style, for example:

  • Display output: views-view.tpl.php
  • Style output: views-view-STYLE.tpl.php
  • Row style output: views-view-fields.tpl.php
  • Field content: views-view-field.tpl.php

etc.

Each of those templates has its own preprocess function. What you do in your code is only affecting the display output since you are preprocessing only the views-view.tpl.php (template_preprocess_views_view)

To find out which template files are available to your specific view, you can go to view edit page and under 'Advanced' section at the end of the column click on 'Theme: Information'. You will see which template files your view is using (in bold) and which template files you would use to override them.

The preprocess functions should be named after the template file, replacing the dashes with underscores. For example for views-view-STYLE.tpl.php would be template_preprocess_views_view_STYLE().

0

You could use the functions:

  1. drupal_set_title()

    Sets the title of the current page.

  2. drupal_get_title()

    Gets the title of the current page.

  • Thanks, but my view generates a block rather than a page, so this is not a solution. Moreover, that would require passing a string argument, like t('My Views Title') and then translate it again on the admin side. Which is what I want to avoid (yet I have already translated that View...). – Artur Dec 17 '12 at 15:22
0

If I remember right you can pass the title with the view object, add a new variable inside the view object and ust in the template file.

0

To address the specific question of "pass a variable to a template file"... I think you just have to use a different hook. Attach the variable to your $view object like this:

function NAME_OF_MODULE_views_pre_render(&$view) {
  $view->display_title = "What have you";
}

Then in a template file like views-view--NAME_OF_VIEW--page.tpl.php:

print $view->display_title;

Just had to do this myself and it worked great.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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