1

Taxonomy terms have a canonical path of /taxonomy/term/{taxonomy_term} by default, which displays a View with content tagged with that term.

For certain taxonomies I would like to use a different view, dependent on the taxonomy bundle, so I can't just override the path entirely.

For example, "article" tags should link to /articles/{taxonomy_term}, "product" tags to /products/{taxonomy_term}.

My idea is to override the entity.taxonomy_term.canonical route adding some kind of callback that generates the path dynamically based on the taxonomy_term. Is this possible?

3
  • 1
    Taxonomy terms have a canonical path of /taxonomy/term/{taxonomy_term} by default, which displays a View with content tagged with that term. That's not quite right - the term page is powered by a template like any standard entity. There is also a View included with core (disabled by default) that can take over that path. But they're different things. If you're using the View, and you override entity.taxonomy_term.canonical, nothing will happen - the View's route will win – Clive Feb 2 at 12:03
  • True, but I think the taxonomy View is activated by default, no? – Tim Feb 2 at 13:28
  • 2
    I guess it depends which installation profile you use – Clive Feb 2 at 13:29
2

I don't believe that there is any need for custom route manipulation in your case, and besides, while I am not entirely sure, I don't think that something like what you describe, would actually work, since then you might have issues with the view's contextual filtering.

I believe the TVI Module is the answer to your problem. It can be used to specify the view/views that will be used in the vocabularies of your choice.

2
  • Thanks for the tip. They're manipulating the route to use a customer controller that renders the required view, but I guess that the path stays the same. I basically want to rewrite the URLs for certain taxonomy tags, so maybe an outgoing path processor... – Tim Feb 2 at 13:28
  • 2
    Well i guess you could, but i don't understand why you should. If you do indeed install the module... you can solve your issue with some clicks. Then again if you will have a bunch of views and/or want a more dynamic approach to the view naming, then i guess you are right. But I really think that in any other case, I would just install the module, since the extra work would not justify the gain of implementing something more "lightweight". In short: Unless you have a strict policy on contrib modules, I would recommend just using the TVI module to achieve your goal. Best Regards, hope I helped. – M.Anagnostopoulos Feb 2 at 14:37
1

I used a path processor service to rewrite the outgoing paths for taxonomy terms that belong to a specific bundle.

<?php

namespace Drupal\my_module\PathProcessor;

use Drupal\Core\PathProcessor\OutboundPathProcessorInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\Request;

/**
 * Path processor to redirect taxonomy paths to a view with contextual arg.
 */
class TaxonomyPathProcessor implements OutboundPathProcessorInterface {

  /**
   * {@inheritdoc}
   */
  public function processOutbound($path, &$options = [], Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL) {
    if (isset($options['entity_type'], $options['entity']) && $options['entity_type'] === 'taxonomy_term') {
      /** @var Drupal\taxonomy\Entity\Term $entity */
      $entity = $options['entity'];
      if ($entity->bundle() === 'article_tags') {
        $url = Url::fromRoute('view.articles.page_1', ['arg_0' => $entity->id()]);
        return $url->toString();
      }
    }

    return $path;
  }

}

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.