0

I have a Views 3 block whose block title I would like to set using PHP in the header section. This would enable me to use tokens from the views results, which is not possible from the UI.

In the case of a views page I was able to set the page title via the following PHP command in the header section:

$view = views_get_current_view();
$view->build_info['title'] = 'My Page Title';

However, I was not able to do the same thing for block titles via

$view = views_get_current_view();
$view->build_info['title'] = 'My Block Title';

This is probably because the build_info['title'] command only edits the page (or node) title. Does anyone know of an analogous command to set block titles?

  • Both the page and the block are subviews of a view (or a collection of views). The page view is the master view. Maybe views_get_current_view() only loads the master view? Is there a command to load a subview? Something like views_get_current_view()->subview? – deinqwertz Oct 25 '12 at 9:07
  • i guess you can change title of view in YOURTHEME_preprocess_views_view(&$vars) – Petro Popelyshko Oct 29 '12 at 13:59
1

Sorry, I made a huge mistake. When I tried to achieve the same thing for the block title, I copied the code from the page header but didn't remove it from the page header. In other words, I used the PHP commands - exactly as described in the question - in the headers of two subviews of the same view simultaneously. To be more precise, the variable $view was used in two places, which caused an error.

When I replace

$view = views_get_current_view();
$view->build_info['title'] = 'My Block Title';
with
$view_block = views_get_current_view();
$view_block->build_info['title'] = 'My Block Title';
things work as desired!

1

In the current version of Views (3.14), you can write tokens in the view title.

It doesn't say so in any help messages, but I have tested it on https://simplytest.me and it worked.

You can see the available token names by selecting the last field and activating Rewrite the output of this field and folding out Replacement patterns.

  • This just saved me so much headache. I wish I could upvote this like 20 times. – beth Apr 6 '17 at 3:34

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.