0

I am having the issue of overriding the default taxonomy controller /taxonomy/term/{tid}.

I have created my own controller which takes {tid} as a parameter and I want to use it instead, so I overrode the default taxonomy controller in the RouteSubscriber like this:

    <?php

namespace Drupal\my_module\EventSubscriber;

use Drupal\Core\Routing\RouteSubscriberBase;
use Drupal\Core\Routing\RoutingEvents;
use Symfony\Component\Routing\RouteCollection;

class RouteSubscriber extends RouteSubscriberBase {

  protected function alterRoutes(RouteCollection $collection) {        
    foreach ($collection->all() as $route) {          
      if ($route = $collection->get('entity.taxonomy_term.canonical')) {
        $route->setDefault('_controller', '\Drupal\my_module\Controller\PagerController::handleDefaultPage');        
      }
    }
  }


  public static function getSubscribedEvents() {
    $events = parent::getSubscribedEvents();
    $events[RoutingEvents::ALTER] = array('onAlterRoutes', -250);
    return $events;
  }

}

My sample page controller is this:

<?php

namespace Drupal\my_module\Controller;

use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Class PagerController.
 */
class PagerController extends ControllerBase {

  /**
   * Handlepage.
   *
   * @return string
   *   Return Hello string.
   */
  public function handleDefaultPage($tid) {
    return [
      '#type' => 'markup',
      '#markup' => 'term: ' . $tid,
    ];
  }
}

And its routing is this:

my_module.pager_controller_handleDefaultPage:
  path: '/taxonomy/term/{tid}'
  defaults:
    _controller: '\Drupal\my_module\Controller\PagerController::handleDefaultPage'
    _title: 'Taxonomy Pager'
  requirements:
    _permission: 'access content'
    _entity_access: 'taxonomy_term.view'
    taxonomy_term: \d+

But then, when I open the taxonomy page, I get this error which means, that I need to pass the $tid param somehow.

RuntimeException: Controller "Drupal\my_module\Controller\PagerController::handleDefaultPage()" requires that you provide a value for the "$tid" argument. Either the argument is nullable and no null value has been provided, no default value has been provided or because there is a non optional argument after this one. in Symfony\Component\HttpKernel\Controller\ArgumentResolver->getArguments() (line 78 of /var/www/html/vendor/symfony/http-kernel/Controller/ArgumentResolver.php).

How to resolve it?

0

Actually, I have found an error in my code. Everything works normally, when the param name and type are set properly. So the final code should be:

My sample page controller is this:

<?php

namespace Drupal\my_module\Controller;

use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\taxonomy\TermInterface;

/**
 * Class PagerController.
 */
class PagerController extends ControllerBase {

  /**
   * Handlepage.
   *
   * @return string
   *   Return Hello string.
   */
  public function handleDefaultPage(TermInterface $taxonomy_term) {
    return [
      '#type' => 'markup',
      '#markup' => 'term: ' . $taxonomy_term->getName(),
    ];
  }
}

And its routing is this:

my_module.pager_controller_handleDefaultPage:
  path: '/taxonomy/term/{taxonomy_term}'
  defaults:
    _controller: '\Drupal\my_module\Controller\PagerController::handleDefaultPage'
    _title: 'Taxonomy Pager'
  requirements:
    _permission: 'access content'
    _entity_access: 'taxonomy_term.view'
    taxonomy_term: \d+

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.