1

I'm using views to link a menu-item to the last node added of a certain content-type.

To accomplish this, I've set up a view to display a 1 full node sorted by post date.

this all works just fine, but now I want to use the node title as page title () instead of the view page title. I also don't want the node title to link to the node again.

Does anyone know how I can do this?

Thx in advance.

5

I finally managed to pull this off by writing a small module using following code:

function MYMODULE_preprocess_views_view(&$vars) {
  // Get my view
  if ($vars['view']->name == 'MY_VIEW_NAME') {
    //get the title of the node shown in the view
    $nid = $vars['view']->result[0]->nid;
    $node = node_load($nid);
    $my_title = $node->title;

    /* Code to dynamically set title here...
       Override view's title since drupal_set_title() is not working.*/
    $vars['view']->build_info['title'] = $my_title;
  }
}

This module changes te views page title and uses the nodes title as page title instead.

Now you can remove the linked nodetitle in the node--YOURTYPE.tpl.php

| improve this answer | |
  • @Alex Will do so, but is says that I have to wait 1 more day. – jeroen Jun 18 '12 at 8:02
1

This is a great way if you want to address all displays of a view, but what if you want this only for one specific display?

I found an answer to this on http://dropbucket.org/node/194.

// first implement hook_preprocess_views_view() which is called for each view.
function HOOK_preprocess_views_view(&$variables) {
  if (isset($variables['view']->name)) {
    $function = 'HOOK_preprocess_views_view__'.$variables['view']->name; 
    if (function_exists($function)) {
     $function($variables);
    }
  }
}

function HOOK_preprocess_views_view__MY_VIEW_NAME(&$variables) {
  //alter here that specific view
  if($variables['display_id'] == 'page_1') {
  // here it will alter specific display of that view.
    //get the title of the node shown in the view
    $nid = $variables['view']->result[0]->nid;
    $node = node_load($nid);
    $my_title = $node->title;

    /* Code to dynamically set title here...
       Override view's title since drupal_set_title() is not working.*/
    $variables['view']->build_info['title'] = $my_title;
  }
}

Now only the page_1 display of a view gets it’s title from the node. All other displays just get the title defined in the display.

| improve this answer | |
  • thx for your addition – jeroen Aug 20 '13 at 5:22

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.