0

I have a content type of node entity, when I follow /node/123 I want my custom controller to be applied. I created a route, but Drupal ignores it.

entity.node.my_route:
  path: '/node/{node}'
  defaults:
    _title: 'Collection'
    _controller: '\Drupal\my_module\Controller\PropertiesController::collection'
  requirements:
    _permission: 'access content'
  options:
    no_cache: 'TRUE'
    parameters:
      collection:
        type: entity:node

In twig template I call it like:

<a href="{{ path('entity.node.my_route', {'node': node.id}) }}">

I also tried

<a href="{{ path('entity.node.canonical', {'node': node.id}) }}">

What am I doing wrong?
Also it a little bit confuses me when I do following and visit /node/123/test:

entity.node.my_route:
  path: '/node/{node}/test'
  defaults:

My custom controller get loaded.

3
  • 2
    You can't just add another route for the same path, this doesn't override the core route. You have to alter the existing route, see drupal.stackexchange.com/questions/215141/…
    – 4uk4
    Jul 25, 2022 at 16:49
  • @4uk4 thanks! I think this is what I need. I suggest leaving this post open because I wasn't sure how to google this kind of problem and how it can be possibly named. 'How to custom controller for a content type' sounds legit to me.
    – mevsme
    Jul 25, 2022 at 17:02
  • @mevsme We still need to close it, just so new answers don't get posted here rather than on the canonical post. But its closed as a duplicate so it'll stay around in the SERPs for people who search for the problem with the same phrasing as you did
    – Clive
    Jul 25, 2022 at 20:26

0

Browse other questions tagged or ask your own question.