0

I have set up a View that shows a random page from the site filtered according to node types and uses the Global : Random (asc) sort criteria. It displays only one result per page and a fresh page is shown every time the page is refreshed.

I want to set the view's page title to be same as the node displayed. I am doing this using javascript right now, but would prefer a PHP alternative that I could either use in the template or the views setting directly.

Setting the page title using javascript isn't SEO friendly.

I am using the latest version of Drupal 6 with the latest stable release of Views 3.

The results of print_r($view->result, true) (as requested) are :

Array
(
    [0] => stdClass Object
        (
            [_random] => 0.00171253550957193
            [radioactivity_node_1_s_energy] => 3
            [node_title] => <Title of the node being displayed>
            [nid] => 393
        )

)
1

Inside a views template, you could call drupal_set_title() with whatever you want out of the $view->result array. You can put a drupal_set_message('<pre>' . print_r($view->result, true) . '</pre>'); or its equivalent in there to see what you have to work with.

In Style Settings, just click on Theme Information, and choose one, I'd suggest modifying the Style Output one because you could also change $title in there as well.

ADDITIONS DUE TO COMMENTS:

No, views will not set this for you, I thought that's what you were asking.

In your views template for the view in question, you need to dig into your row result to get the value you want and then call drupal_set_title() with that. Calling it with a null value only returns the existing title, if any.

For example, if you put the print_r() of your result in there, you will see what values you have to work with, and then you use them with this to set the title.

Let's say you have something like this:

[0] => stdClass Object
    (
        [node_title] => Sirens
        [nid] => 240
        [_random] => 0.000554758124570137
    )

which is the view result from a view I just put together that randomly returns node titles, you would do something like this in your template:

$title=check_plain($view->result[0]->node_title);
drupal_set_title($title);

You could also put this in a pre_render() hook like so:

function YOURMODULE_views_pre_render(&$view) {

  if ($view->name==NAMEOFYOURVIEW) { // && $view->current_display == 'page' if needed
    $title=check_plain($view->result[0]->node_title);
    drupal_set_title($title);
  }

}

But either way, since I don't know exactly what your view row looks like, I can't tell you precisely what $view->result[0]...... you need.

Also, all of this is assuming your theme behaves nicely with the $head_title variable as well.

  • I tried adding <?php drupal_set_title(t($title)); ?> and <?php drupal_set_title($title); ?> in the style output template, separately in different attempts. None of the approaches worked. – bcosynot Apr 27 '12 at 17:56
  • what did you set $title to? – Jimajamma Apr 27 '12 at 18:23
  • I did not set it to anything. Isn't that set by the View? – bcosynot Apr 27 '12 at 18:35
  • I did whatever you mentioned here, and it works, except it's still not changing the document title. I thought drupal_set_title() does the same. Can the pre_render() hook be added to my theme's template.php? – bcosynot Apr 27 '12 at 19:23
  • I've only used the pre_render() hook within a module, and a quick test here didn't work by naming it MYTHEME_views_pre_render() and putting it in my template.php file, so if you don't have a custom module, putting the code in your views template looks to be the only way to go. I would put a $title='foobarbaz'; set_drupal_title($title); or something in there to make sure your template is being called as all of the above worked here. Also, if you want to edit your question with a print_r() of your $view->result, that would help out as well. – Jimajamma Apr 27 '12 at 19:40

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.