Alex W
Alex W

Reputation: 38193

Drupal 7 Views $title Variable

I have a view on a blog page in Drupal 7 that I am overriding using a ".tpl.php" or template file so that I can use my own custom HTML and add a title.

Normally, the title of the view can be accessed in the template file using $title or $block->subject but this is not working in my view template file.

Using drupal_get_title() simply displays "Blog" because the page itself is a page template, but I need the title of the block view that is being inserted dynamically with PHP into the page template file.

The weird part is that the title will show in the view preview along with the results of the database query (a.k.a. the fields).

So, how do you access the variable that is normally called $title in the template PHP file?

Upvotes: 1

Views: 6838

Answers (1)

Alex W
Alex W

Reputation: 38193

First, on the view settings page go into "Theme: Information" under the "Other" section. There you find a list of the file names that can be used to override your view output.

So, if your view is called "Test View" then you should create a file called views-view--test-view.tpl.php inside your your_theme/templates/views/ folder to override the view's display settings.

In that file, put the following:

<?php if($view->human_name) print "<h2>" . $view->human_name . "</h2>"; ?>

Then under "Theme: information" click the button that says "Rescan template files," and the title should show up now wherever that view is being shown on your site.

If that variable is coming up blank, you can get a list of guesses for what to variable to display by putting the following code in that file:

<?php
?>
<div>
    <?php $list = array_keys(get_defined_vars()); 
    foreach($list as $key => $value)
        print "$" . $value . ", ";
    die();

    ?>
</div>

Each of those variables might be an object or array so you may have to print their content with print_r($variable) or var_dump($variable) to get an idea what is inside of them.

Upvotes: 3

Related Questions