-3

I need to show the same content with different urls.

I would like to create dynamic routes when saving a node in Drupal 9.

Does this seem the right way?

Some other solution or similar module.

A specific use case:

node/1 -> 'How to sign up'

url -> '/actor/how-to-sign-up', '/supporter/how-to-sign-up'

P.S. I need the first part of the path (actor,supporter) for various checks

2
  • 1
    I think you need to elaborate on the use case. You need multiple dynamic routes for the same node? What should determine the urls? But you can try look into PathAuto.
    – NicklasF
    Commented Sep 30, 2021 at 6:56
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – Community Bot
    Commented Sep 30, 2021 at 19:17

1 Answer 1

1

The only example in Drupal core for a dynamic route depending on user saved data is a View with a page display and this takes a very long time to save. Views needs routes for contextual filters. Unless you have such a specific use case for content (please edit your question if you have one), the right way would be aliases. In the node edit form you can add only one alias, but you can add more in /admin/config/search/path. If you want that in one place you could alter the node edit form for multiple aliases.

If you don't want to create each alias by hand and the paths have a pattern you can use a path processor. You can create the scaffolding with drush

drush gen path-processor

and put the regex pattern in the inbound method:

/modules/custom/mymodule/src/PathProcessor/PathProcessorMymodule.php

<?php

namespace Drupal\mymodule\PathProcessor;

use Drupal\Core\PathProcessor\InboundPathProcessorInterface;
use Symfony\Component\HttpFoundation\Request;

/**
 * Path processor to remove a prefix.
 */
class PathProcessorMymodule implements InboundPathProcessorInterface {

  /**
   * {@inheritdoc}
   */
  public function processInbound($path, Request $request) {
    return preg_replace('#^/(author|supporter)/#', '/', $path);
  }

}

You can get the original path later with

\Drupal::request()->getPathInfo()

or you could store the prefix in a service property before removing it to make it globally available.

If your checks produce rendered output then add the url.path cache context.

0

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