5

I have a view that uses a contextual filter for part of the URL. For example: www.example.com/categories/%/all

I have successfully replaced the % with the name of the taxonomy term from a specific category (for example, www.example.com/categories/shirts/all), but I cannot get the Title Override options to work.

Here's how I have things set up...

Contextual Filter - Indexed Content: Clothing Categories
When the filter value is NOT in the URL -  Display empty text 
Override title - %1
Specify validation criteria: Validator: Taxonomy Term
Filter value type - Term Name Converted to Term ID
Transform dashes in URL to spaces in term name filter values  (checked)

When I preview the View result by typing the contextual filter "shirts", it does display the shirts and it does display the title "Shirts". However, on the live page, the View's title is displayed instead ("Category View"). I have tried deleting the View's title, but that just causes the front-end page to display no title.

Any idea on how I can get this to work, or any other methods to make it work?

Thanks!

3 Answers 3

4

In Drupal 8, if you are using a contextual filter to pass a taxonomy term to a View and want to change the page title to match with the term title, you can:

/**
 * Implements hook_preprocess_views_view().
 */
function HOOK_preprocess_views_view(&$variables) {

  $id = $variables['id'];

  if( $id == 'YOUR_VIEW_NAME' && $variables['view']->args[0]) {

    // Get tid from contextual argument of the view
    $tid = $variables['view']->args[0];

    // Load the Term Entity
    $term = Term::load($tid);

    // Term Title
    $new_title = $term->getName();

    // Set the new Page Title
    $request = \Drupal::request();
    if ($route = $request->attributes->get(\Symfony\Cmf\Component\Routing\RouteObjectInterface::ROUTE_OBJECT)) {
      $route->setDefault('_title', $new_title);
    }
  }
}
3

After hours of searching, trial and error, and then posting this question, I came across the correct method minutes later: Get term name for page title from term id passed in the contextual filter?

do the following for this field:

Check "Provide default value".
Select Type: "Taxonomy term ID from URL"
Select checkbox: "Load default filter from term page"
0

I'm using Drupal 8, this worked for me.

In /core/modules/views/views.theme.inc file I appended following code to template_preprocess_views_view method:

$view = $variables['view'];
$id = $view->storage->id();

...
...
...

if( $id == '__VIEW_ID__' && $view->args[0]) {

    // Get tid from contextual argument of the view
    $tid = $view->args[0];

    // Load the Term Entity
    $term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($tid);
    // $term = Term::load($tid);

    // Term Title
    $new_title = $term->getName();

    // Set the new Page Title
    $request = \Drupal::request();
    if ($route = $request->attributes->get(\Symfony\Cmf\Component\Routing\RouteObjectInterface::ROUTE_OBJECT)) {
      $route->setDefault('_title', $new_title);
    }
 }

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.