4

I have created a module with its routing.yml file.

mymodule.dossier:
  path: '/dossiers/{node}/dossier'
  defaults:
    _entity_view: 'node.dossier_dossier'
  requirements:
    _permission: 'access content'

I have created a custom view mode (node.dossier_dossier) and the corresponding Twig template. If I go to dossiers/1/dossier, it works, and it displays the node with the dossier_dossier view mode and appropriate template.

node/1 has /dossiers/my-custom-path as its path alias, which is automatically set from the Pathauto module. If I try to access /dossiers/my-custom-path/dossier, it returns a Page not found error.

How can I make this route work for my nodes for both (default and path aliases) URLs?

Edit: I should maybe mention why I need that:

  • /dossiers/my-custom-path should display the node using the default view mode
  • /dossiers/my-custom-path/dossier should display the node using the dossier_dossier view mode
  • /dossiers/my-custom-path/abstract should display the node using yet another view mode (I'd create another route for that one presumably)
4
  • Since the path alias already contains the /dossiers part of the path, I also tried with path: '/{node}/dossier' but that didn't work either.
    – MrUpsidown
    Commented Jul 27, 2016 at 10:02
  • The aliases is only valid for the full url, not parts of it. If you want /dossiers/my-custom-path/dossierto work, you have to use the complete url as alias.
    – 4uk4
    Commented Jul 27, 2016 at 10:15
  • @4k4 could you please elaborate a bit more? I don't much understand your comment. Also please see my edit in the question where I explain the goals.
    – MrUpsidown
    Commented Jul 27, 2016 at 11:01
  • 1
    {node} refers to the node's ID, which is then upcast to a node object for the controller. It doesn't refer to any URL alias you may have for the node. To get what you're looking for, you'd need a custom upcaster that searched for and found the node based on its alias
    – Clive
    Commented Jul 27, 2016 at 11:37

2 Answers 2

2

The subpathauto module (depends on pathauto) might help you here: https://www.drupal.org/project/subpathauto

I have a pathauto alias on my site for /node/{id} (let's call it /node-slug). By installing and enabling the subpathauto module, I was able to create a custom route /node/{id}/locations which is now accessible via /node-slug/locations

1

Pathauto can't do this, it can only generate aliases to the default view mode.

You can write your own Pathauto module, which generates for each view mode a separate aliases. For a single purpose module there is not much work to do, you only need to generate the aliases and use

\Drupal::service('path.alias_storage')->save($source, $alias);

to save them.

The problem is, you get a very large url_alias table.

Or use route_callbacks: in *.routing.yml, then define in this callback for each view mode a different route, which calls a Controller, which then in case you have a hit on the route will look up the middle part of the alias and return a redirect to the node in the requested view mode. This is without the static alias table, but may take longer for each request.

Your Answer

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

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