0

I have a regular Controller on the taxonomy term page (the original view is disabled). It works all right to show what I want it to show, however, neither its getTitle nor its access is ever called (the second being more vexing, of course). The problem is not with my controller, because using a different route works just fine:

example.content.documents_list:
  path: '/taxonomy/term/{taxonomy_term}'
  defaults:
    _controller: '\Drupal\example\Controller\DocumentListController::content'
    _title_callback: '\Drupal\example\Controller\DocumentListController::getTitle'
  requirements:
    _custom_access: '\Drupal\example\Controller\DocumentListController::access'

example.content.documents_list2:
  path: '/xxx/{taxonomy_term}'
  defaults:
    _controller: '\Drupal\example\Controller\DocumentListController::content'
    _title_callback: '\Drupal\example\Controller\DocumentListController::getTitle'
  requirements:
    _custom_access: '\Drupal\example\Controller\DocumentListController::access'

I can create a RouteSubscriber if needed but is it really needed? Are those settings never really picked up?

5
  • Guessing your custom access is not properly tagged. Why not use TVI?
    – Kevin
    Jun 28, 2021 at 12:47
  • And it still gets called when the path is any different? Because my needs are drastically different. My controller decides what to show depending on many factors, embedding various blocks or creating tables as the situation dictates. Those are not solvable with views the way I want them to be solved. I'm a programmer, so have no problems with creating content with my own code instead of Views (don't misundestand me, I have nothing against Views and I do have a few on this site, just that there are places where a view is not the best solution).
    – Gábor
    Jun 28, 2021 at 12:52
  • 2
    The View is an override for an already existing core entity route, which is re-enabled when you disable the View. See drupal.stackexchange.com/questions/241880/…
    – 4uk4
    Jun 28, 2021 at 13:06
  • This was the probable solution I hinted at but in the meantime, I tried another approach, and it fails. Isn't the EnhancerInterface applicable here? I can change the incoming $defaults['_title_callback'] to something else but it doesn't make any difference. (I already have an enhancer for a different route, so it was more straightforward just to try adding another condition there).
    – Gábor
    Jun 28, 2021 at 13:13
  • Oh dear, oh my, I already had a subscriber, I forgot when I added it, just that it only set the _controller not the others... :-) I'll copy it to an answer for eternity. Thanks for the right pointer.
    – Gábor
    Jun 28, 2021 at 13:25

1 Answer 1

1

It was partly my mistake, I did have a RouteSubscriber already in place but not fully engaged yet, as the comment of 4k4 pointed out. The final solution is: yes, it has to be overridden because it's a system route:

class RouteSubscriber extends RouteSubscriberBase {

  protected function alterRoutes(RouteCollection $collection) {
    if ($route = $collection->get('entity.taxonomy_term.canonical')) {
      $route->setDefault('_controller', 'Drupal\example\Controller\DocumentListController::content');
      $route->setDefault('_title_callback', 'Drupal\example\Controller\DocumentListController::getTitle');
      $route->setRequirement('_custom_access', 'Drupal\example\Controller\DocumentListController::access');
    }
  }

}
8
  • To clarify, you use the route subscriber to alter the existing routes instead of defining your routes via my_module.routing.yml?
    – sonfd
    Jun 28, 2021 at 13:37
  • 1
    I have both. Belt and braces, you know. :-) But now you made me curious, I check if just the subscriber is enough. Answer: the subscriber on its own is enough. The .yml on its own isn't.
    – Gábor
    Jun 28, 2021 at 13:39
  • The yaml defines it, the alter modifies the existing - that is why. That route already exists.
    – Kevin
    Jun 28, 2021 at 15:33
  • Route subscribers are used to alter a route defined from another module; they are the equivalent of hook_menu_alter() in Drupal 7. The .routing.yml files are the equivalent for hook_menu() in Drupal 7. As with Drupal 7, you don't use both for the same route.
    – apaderno
    Jun 28, 2021 at 16:20
  • One small question or remark then: if I omit the same from .routing.yml, I lose the comfort of my different route name and I have to use the original one throughout my code. Specifying it both there and in the subscriber seems to offer the best of both worlds, even if redundant: I can use my own route name and it still gets overridden.
    – Gábor
    Jun 29, 2021 at 7:29

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.