2

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
  • 2
    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, 2021 at 12:03
  • True, but I think the taxonomy View is activated by default, no?
    – Tim
    Feb 2, 2021 at 13:28
  • 2
    I guess it depends which installation profile you use
    – Clive
    Feb 2, 2021 at 13:29

2 Answers 2

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, 2021 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. Feb 2, 2021 at 14:37
2

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

Service definition:

services:
  my_module.path_processor:
    class: Drupal\my_module\Path\PathProcessor
    tags:
      - { name: path_processor_outbound, priority: 50 }

The processor class:

<?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;
  }

}
2
  • do you mind adding your service declaration ? Thanks :)
    – JFC
    Sep 23, 2021 at 17:05
  • 1
    Added the service declaration. I seem to remember having problems with admin URLs also being overwritten. Not sure if we went with exactly this solution in the end, but only rewrote paths that were using an URL alias.
    – Tim
    Sep 24, 2021 at 8:17

Your Answer

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

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