Target: changing the view title from inside the argument handler PHP code

In Views1 it was simple to change the view title programmatically from inside the Argument Handling Code by using
$view->argument[0]['title'] = 'New title';

I was searching for a solution for Views 2. I did not found any solution in the issue list. So i decided to get my debugger and find the solution.
After some research i found this solution: use $handler->options['title'] = 'MY NEW TITLE';

Detailed description

  1. added view argument, e.g. Term-Id
  2. In radio box Action to take if argument is not present choosen Provide default argument
  3. Set radio box Default argument type to PHP Code and entered return TRUE; in the text area.
  4. Set radio box Validator options to PHP Code
  5. In the textarea PHP validator code entered:
    if( $argument) {
          $handler->options['title'] = 'MY NEW TITLE'; // NOTE String may be calculated from $argument
    }
    return true;
    

    This might be interesting to post in the views 2 documentation.

Comments

lazly’s picture

Version: 6.x-2.0 » 6.x-2.7
Status: Active » Reviewed & tested by the community

Its work, please migrate to doc.

ducdebreme’s picture

Do you have a suggestion, where to put it? ...
... here http://views-help.doc.logrus.com/ ?
Or somewhere else?
Stefan

dawehner’s picture

@ducdebreme
http://views-help.doc.logrus.com/ is a checkout of the advanced-help in views. So you suggest to put it into views, which i think is good. This might be needed often.

Alternative there could be a handbook-snippet-section for views, like for the drupal-5 version, but i prefer the first one, even there are people ignoring advanced help. There should be a more offensive display in views, when the advanced module isn't installed.

lazly’s picture

@ducdebreme

I dont know where is the bigest views doc, maybe somewhere here: http://drupal.org/node/322506

Bye!

merlinofchaos’s picture

Status: Reviewed & tested by the community » Active

RTBC means there's a patch attached.

Also, $handler->set_option('title') will work better than directly modifying the title, since that won't work for overridden displays.

scalp’s picture

Is there a way to insert two arguments into the title? For example, "$argument1 in $argument2". Seems like with the need to change the $handler variable this wouldn't be possible.

ice5nake’s picture

I've tried the following from within hook_views_pre_render

//DOES NOT WORK
$view->display_handler->set_option('title', $title);
//DOES NOT WORK
$view->display[$view->current_display]->handler->options['title'] = $title;
//DOES NOT WORK
$view->display[$view->current_display]->handler->set_option('title', $title);

Can't figure this out. Thanks so much for any insight.

ice5nake’s picture

I moved

$view->display_handler->set_option('title', $title);

to hook_views_pre_build and now it works.

The new problem, it doesn't change the title for feed display types.

merlinofchaos’s picture

Maybe setting the title in the $view->build_info would work? The title does get set there, I think, or at least information about it does. I thought.

ice5nake’s picture

@merlinofchaos The title is how I want it in the build_info. So that's not where the problem is.

ice5nake’s picture

Scratch that. This is exactly where it needed to be changed.

dawehner’s picture

Category: task » support

This is techical a support issue

summit’s picture

Subscribing, needing to be able to change title of node_view to the related taxonomy term name.

greetings, Martijn

xjm’s picture

Version: 6.x-2.7 » 6.x-2.11

I tried #7, #8, and #9 -- none seemed to work. The hooks are called, but nothing I try seems to change the page title.

I need to be able to set the page title dynamically based on the result set, so I'm not sure hook_views_pre_build() would allow that anyway.

I know that there were a number of API changes in 2.8; is it possible that there's a different method to use in the current release?

xjm’s picture

Here's an alternate solution. Instead of setting the view title, I just overrode the title with the core API.

function mymodule_views_pre_render(&$view) {
  if ($view->name == 'myview' && $view->current_display == 'mydisplay') {
    $title = '';
    // Your code to set $title here
    drupal_set_title($title);
  }
}

Not sure what other repercussions this might have, and I guess it only will work for a page view, but it's the only thing I've tried so far that actually changed the title.

dawehner’s picture

Status: Active » Fixed

I think this is fixed now. There are serveral ways provided how to do it.

xjm’s picture

Status: Fixed » Active

#16: No, none of the ways provided work in 6.x-2.11, at least not in any view I've tested. My solution is a workaround and will not work for anything other than the main page view.

Letharion’s picture

Assigned: Unassigned » Letharion
Category: support » task

Assigning to myself as I'm gonna try to get this into the docs.

glass.dimly’s picture

bump.

where is this?

goofrider’s picture

Found a solution that works.

http://www.zyxware.com/articles/644/change-the-page-title-of-a-view-in-d...

BTW it has to be in the full form (note the code reference a specific page display):

    $view->display['page_1']->handler->handlers['argument']['nid']->options['title'] = 'Your custom page title';

I tried to use:

    $handler->argument->options['title'] = 'Your custom page title';

But that doesn't work. Maybe $handle->argument is not an object.

Steven Brown’s picture

I am currently using panels to display two views on a single page. The only suggestion from above that will
change the title for me is the following.

function hook_view_pre_build(&$view) {
  $view->display_handler->set_option('title', $title);
}

Only issue with this is it changes both of the view titles. I want to be able to change each title. I have one view but two different displays which is why I am assuming the above code doesn't work. Seems to apply it to all display titles.

If anyone has an idea to do what I'm asking please let me know.
IRC Name: FatGuyLaughing

Thanks in advance

merlinofchaos’s picture

Test $view->current_display and set the id based upon display title?

Steven Brown’s picture

Alright I have found out how to do this in case anyone else wants to know.

First make a module. In you mymodule.module file put the following code.


/**
 * Implementation of hook_views_pre_build().
 */
function mymodule_views_pre_build(&$view) {
  if ($view->name == 'view_machine_name') {
    $view->display['display_machine_name']->handler->options['title'] = 'New Display Title';
  }
}

Components:
Where it says 'view_machine_name' this is the name that you give the view when you create a new one.
Where it says 'display_machine_name' this is the name that views has given it's display. More than likely you don't know what this is. In order to find out just put a drupal_set_message('

' . print_r($view, TRUE) . '

'); statement at the beginning of the hook_views_pre_build() function. Now visit the page that has this view being displayed. Follow the hierarchy of the view object. You printed out $view so don't look for that. Instead look for the display array. In that array will be display objects. Each of these have a name. Find the one that is your display. If the display is a page display then the naming convention is this page_#. So page_1, page_2, etc.

The reason for checking that you have the right view object is so you don't get any errors. This is the error you will get if you don't check.

Error:
Fatal error: Call to undefined method stdClass::attach_to() in /var/aegir/platforms/worx_business-6.x-1.0-alpha4/profiles/worx_business/modules/contrib/views/includes/view.inc on line 1008

If you use the function hook_views_pre_render() and you don't check the view you will get an error that deals with stdClass::destroy()

Hope this helps guys.

Steven Brown’s picture

#22, Sorry merlin I've been trying to write the #23 comment for a couple of hours or so and didn't refresh to look if someone had posted back.

reinis.berzins’s picture

Does anyone have an idea how to change the MENU TAB TITLE using the same method?

I tried the following three ways, but apparently none of them is correct:
$handler->options['menu']['title'] = 'MY NEW TITLE';
$handler->tab_options['title'] = 'MY NEW TITLE';
$handler->menu['title'] = 'MY NEW TITLE';

Steven Brown’s picture

I don't believe this will work due to the view creates the menu item. After that I don't believe you can override it from the $view object. I believe at this point you would need to use hook_menu_alter() in order to change the title. However, if you are already trying to do this through the $view object then you are in code so it would be easier to do the hook_menu_alter().

<?php
/**
 * Implementation of hook_menu_alter().
 */
function MYMODULE_menu_alter(&$items) {
  $items['path/to/the/menu/tab']['title'] = 'My New Title';
}
?>

Hope this helps!

reinis.berzins’s picture

Thanks! This custom module code works! However it breaks cron, login, save node, flush cache and all other operations (except browsing from page to page) on my website. In the log I find the following error:
Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.

I tried to restrict this custom module to work on certain paths only like node/%, but with IF statements in the code it stops working at all:

function TAB_COUNTER_menu_alter(&$items) {
   if (arg(0) == 'node' && is_numeric(arg(1))) {            <-- IF statement turns the whole code non-working.
	$items['node/%views_arg/questions']['title callback'] = 'TAB_COUNTER_title_callback';
   }
}
function TAB_COUNTER_title_callback() {
  $count = db_result(db_query("SELECT COUNT(nid) FROM {node_comments} WHERE nid = %d", arg(1)));
  return 'Questions ('.$count.')';
}

P.S. I also tried you simpler version with static title $items['node/%views_arg/questions']['title'] = 'Questions';, but the same error happens.

Letharion’s picture

Assigned: Letharion » Unassigned
Category: task » support
Status: Active » Fixed

Before this thread gets any longer, I'm going to point out that it's about setting "View-Title", which is not what the discussion is currently about.

I will close this as fixed, as of #26. Unfortunately I haven't taken the time to document this this.
If you have further questions regarding _the title_, please re-open.

Otherwise open a separate issue.

Status: Fixed » Closed (fixed)
Issue tags: -View title, -Argument Handler

Automatically closed -- issue fixed for 2 weeks with no activity.