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.

  • There are replacement patterns which you can use to change the title. This should basically just work. – Daniel Wehner Aug 2 '11 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. – Andreas Koepke Oct 26 '11 at 5:49
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.

  • Not Exactly what I was looking for at the time but this info is useful. Thanks – Jeremy Love May 22 '12 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. – 75th Trombone Mar 28 '13 at 20:05
  • do not use ever the "php code" text format, the best prectice if you turn it off – golddragon007 Dec 30 '17 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;
    }
  }
}
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.