I want to change the title of the page slighty (that is, the text between the tags in the head) depending on whether or not the current page is a view. So I'd like some code that says something like:

if(is_view)
print $newtitle;

the question is, what can I put as a condition for the if statement?

Comments

jerepops’s picture

well if you are using the views module, you can change the title for that view using the title field or using the argument field. Maybe I didnt understand the question correctly?

archard’s picture

That's true, however this is a special case because I'm using the taxonomy_term view, and I only want the title changed when the view is on certain vocabularies. This is gonna require some php code that I don't think I can enter in the title field for the view.

jerepops’s picture

enter it in the header field on the edit views page and click use php code.
do some if statements and drupal_set_title()....? maybe?

archard’s picture

Hey that worked, thanks!

archard’s picture

For those looking for a solution to this, I was able to change the title of the views page depending on what vocab the taxonomy term was from. Here's how I did it:


$name = drupal_get_title();

$yes = taxonomy_get_term_by_name($name);

if($yes[0]->vid == 1)
drupal_set_title(drupal_get_title() . " Tabs");

taxonomy_get_term_by_name() takes a string and checks if there's a term for that. If there is it loads an object with lots of info associated with it, including the vid. Drupal API FTW.

ericpugh’s picture

Okay. but what if your wanted to change the breadcrumb on a view? (and has nothing to do with taxonomy) Does anyone know a way to test for "view" in php?

ericpugh’s picture

This is all I could find:

I tryed the if statement below in my template: which I fount here (http://stackoverflow.com/questions/247991/displaying-a-drupal-view-witho...)

if (isset($variables['views_ui_list_views']) ) {
	//this is a view!
}

...that didn't work for me?? (maybe because my view is not a list ??)
so the only way I could do it was to check the url of the view. I'd love to hear if someone has a "real" solution to this.

if ( arg(0) == 'search' && arg(1) == 'blogid' && is_numeric(arg(2)) ) {
 //this is the url of my view, therefore a view
}
druvision’s picture

This solution seems to work for drupal 5

Summit’s picture

SUbscribing, greetings, Martijn

gzfelix’s picture

try:

if ($view)
  print $new_title;
KoCo’s picture

if (arg(0) != 'node' && arg(0) == 'FIRS_PART_PAGE_TITLE_OF_VIEW') 
rooby’s picture

You can use the following in your template.php file:

<?php
function phptemplate_preprocess_page(&$vars) {
  // Get the view on the current page if there is one
  $views_page = views_get_page_view();
  $vars['view'] = $views_page->view;
?>

Then you cound use $view in your page.tpl

You can also do more useful things.
As an example, if you want the pager of your view to be outside of your content section you can use:

<?php
function phptemplate_preprocess_page(&$vars) {
  // Get the view on the current page if there is one
  $views_page = views_get_page_view();
  $view = $views_page->view;
  // Theme the pager so it can be used in the page.tpl
  // Code taken from template_preprocess_views_view in the views module's theme.inc
  $exposed_input = isset($view->exposed_data_raw) ? $view->exposed_data_raw : NULL;
  if (!empty($view->pager['use_pager'])) {
    $pager_type = ($view->pager['use_pager'] === 'mini' ? 'views_mini_pager' : 'pager');
    $pager_theme = views_theme_functions($pager_type, $view, $view->display_handler->display);
    $vars['view_pager'] = theme($pager_theme, $exposed_input, $view->pager['items_per_page'], $view->pager['element']);
  }
}
?>

and then in your page.tpl where you want the pager:

<?php if ($view_pager): ?>
  <?php print $view_pager; ?>
<?php endif; ?>
wesruv’s picture

Took me too long to get to the bottom of this, leaving my solution here.

I recommend doing this inside of template_preprocess_page() since we're setting up stuff for the template, not spitting out content.

THEMENAME_preprocess_page(&$variables) {
  // Let's not do work if we don't have to
  if (arg(0) != 'node') {
    $page_view = views_get_page_view();
  }
  if (!empty($page_view)) {
    // This page is driven by a view.
    $variables['is_views_page'] = true;
  }
  else {
    // This page isn't driven by a view.
    // But there could be a block view, or other non-page view on the page.
    $variables['is_views_page'] = false;
  }
}

Then in your page.tpl.php you can do:

<h1>
  <?php if ($is_views_page): ?>
    This is a view <?php print $title; ?>
  <?php else: ?>
    <?php print $title; ?>
  <?php endif; ?>
</h1>