2

I have an 'articles' content type with a node reference field that allows you to select a term from the taxonomy 'category'. I have a views block that gets all the a contextual filter to return the titles of all the 'articles' with the same category as the article being viewed. I would like the title of the views block to read 'More name of category articles'. I have tried using 'More %1 articles', but this returns 'More 15 articles'. I'm guessing that this is the taxonomy term number?

Is there a way get the taxonomy name filtered by and use it in the view title? This would be really elegant. I guess the alternative would be to do something with the views 'header' and hide the views title?

Thank you in advance.

3
  • what does the url of the page where the block is used look like?
    – 4life
    Commented Jun 6, 2013 at 13:42
  • Hi 4life the url for these pages is in the format: domain.co.uk/our-work/taxonomy-term/node-title-of-article Commented Jun 6, 2013 at 15:01
  • On what field of your contextual filter did you set the More %1 articles
    – 4life
    Commented Jun 6, 2013 at 15:22

2 Answers 2

2

Yes, you can get the taxonomy term name and use it in the view title. It's possible with the contextual filter settings. You can achieve your goal in two ways:

Option #1 ( Using "%1" for the first argument, "%2" for the second, etc. )

When the filter value IS available or a default is provided:

Override title -> More %1 articles

Specify validation criteria -> Taxonomy term

Filter value type -> Term ID

Action to take if filter value does not validate -> Hide view

Option #2 ( using PHP code )

When the filter value IS available or a default is provided:

Override title -> check this option, but leave text field empty.

Specify validation criteria -> PHP Code

Enter the code:

$tid = $handler->argument;
$term = taxonomy_term_load($tid);
if ($term){
  $name = $term->name;
  $handler->options['title'] = "More $name articles";
  return true;
}
else{
  return false;
}

Action to take if filter value does not validate -> Hide view

4
  • Thanks Jack. This worked perfectly! Boriana - thank you for your answer too. It looks like it would work well, but I thought it was more elegant to use the view title. Commented Jun 7, 2013 at 8:35
  • You're welcome. I'm glad I could help. Ps. I edited my answer, because I just realized it can be done without PHP as well.
    – Jack-PL
    Commented Jun 7, 2013 at 12:58
  • Even slicker! - good job. However I'm going to keep using the original php method because my taxonomy name had a capital letter so 'More Taxonomy name articles' looked odd. So i added $name = strtolower($name); to your original php to get it to lower case. Do you have a copy of your old response? As it was a working answer it would be good to leave it up here in case it is more appropriate for some people to use than your admittedly slick solution above. Thanks again :) Commented Jun 7, 2013 at 13:43
  • You're right. The solution with PHP code might be useful as well. I edited my answer and put my old response back, so the people would compare them and choose the appropriate one.
    – Jack-PL
    Commented Jun 7, 2013 at 15:39
1

The only way I think you can do this is by using the views header (as you suggest) because you can actually use php there to get the term name from the term id that's provided.

If you have the 'PHP filter' module enabled on your site (it comes with Drupal core, but I wouldn't enable it unless I have to, and make sure you don't give access to the 'php code' text format to any roles).

In any case, if you have that module enabled, you'll be able to put php code into the view header (make sure you select 'PHP code' as the text format). Then you can get to the term name like this:

<?php
$term = taxonomy_term_load(%1);
$name = $term->name;
return ("<h2>More $name articles</h2>");
?>

I just tested it and it should work pretty well. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.