Create a view to display content of a taxonomy term, which also displays the taxonomy term as the page title in the correct language

Events happening in the community are now at Drupal community events on www.drupal.org.
freeform.steph's picture

By default, taxonomy pages display content in chronological order, and this is not customizable unless you update the core module.

To get around this, create a view with taxonomy term as the argument, simple (if you're new to views and arguments, check-out this tutorial video: http://gotdrupal.com/videos/drupal-views-arguments).

In the argument settings, if you enter %1 as the title override, the page title will become the Taxonomy term of the taxonomy id argument, BUT this method fails to show the translated string if you're not viewing the page in the default language.

To get around this, instead of using the argument title override, use php in your view's header setting:

<?php
// get language properties of current language
global $language;
$lang = $language->language;

// get the argument from view and get the term properties
$view = views_get_current_view();
$tid = $view->args[0];
$term = taxonomy_get_term($tid);

// if term exists, get term name in current language
if($term){
$name = $term->name;
$mytitle = tt("taxonomy:term:{$tid}:name", $name, $lang,FALSE);
}
else {
// provide a default title if no argument was given, replace "default_title" with desired title
$mytitle = t(default_title);
}
print
"<h1 class='title'>".$mytitle."</h1>";
?>

Comments

Where can I find my 'views header section' ?

LukM's picture

I have the same problem, but I cannot find the place where to apply this modification? Where can I find theis 'view's header setting'?

Thank you !

tomsm's picture

After upgrading to views version 6.x-3.x-rc2 my header code:

<?php
drupal_set_title
(t(drupal_get_title()));
?>

did not work anymore.

But yours does !

Update, 27 april 2012:

I have changed

<?php
print "<h1 class='title'>".$mytitle."</h1>";
?>

to
<?php
$view
->build_info['title'] = $mytitle;
?>

Now the html title tag in the page source is correctly filled.

Views Developers

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: