6

Is it possible to get a taxonomy term field in the title of a view, using the override title in the contextual filters? I know I can get taxonomy fields using relationships, but is there an argument or something similar that I can use in the override title?

I am asking for Drupal 7.

2
  • There are replacement patterns which you can use to change the title. This should basically just work. Aug 2, 2011 at 10:00
  • Just wanted to add to the above. You don't need to use the "php code" text format in order to use replacement patterns. The "php code" formatter is only used if you want to execute custom PHP code. Oct 26, 2011 at 5:49

4 Answers 4

2

I just recently solved this problem by hiding display of the default title in views, adding the field "content all: taxonomy term" in views. Then on the "header" section of views add "global:text area" and select the text format "php code". check "use replacement patterns from the first row". You can click on "replacement patterns" to view all of your tokens. In the text area box paste the code: <h1>[term_node_tid]</h1> Your term name should replace the default title.

Hope this helps.

3
  • Not Exactly what I was looking for at the time but this info is useful. Thanks May 22, 2012 at 21:52
  • This will fail badly if there is more than one taxonomy term on the first node returned by the view. It will often use one of the terms not referred to in the Contextual Filter. The below answers are more reliable in these cases. Mar 28, 2013 at 20:05
  • do not use ever the "php code" text format, the best prectice if you turn it off Dec 30, 2017 at 16:40
4

I was able to solve a very similar issue in which I needed to override the view title with a field from the first result.

I used hook_views_pre_render() to accomplish this in drupal 7.

function tag_views_pre_render(&$view) {
  if( $view->name == 'flex' ){
    if ( count($view->result) ){
      $view->build_info['title'] = $view->result[0]->node_title;
    }
  }
}
0
2

the only solution I found (thanks to FatGuyLaughing: http://drupal.org/node/365418#comment-4284154) was the following:

function MYMODULE_views_pre_build(&$view) {
  if ($view->name == 'view_machine_name') {
    if (is_numeric(arg(1)) && arg(1) > 0) {
      $term = taxonomy_term_load(arg(1));
      if ($term) {
        $view->display['default']->handler->options['title'] .= ' - ' . $term->name;
      }
    }
  }
}
0

If you use contextual filter "Content: Has taxonomy term ID (with depth)" and title overriding, just set "%1" and the title (aka name) of the term is set as title of the view.

Note that this doens´t work with Filter by Field "Content: Vocab XYZ (field_vocab_xyz)". In this case the term ID is set as title.

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.