Score:-3

Show the same content with different urls

th flag

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

NicklasF avatar
us flag
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](https://www.drupal.org/project/pathauto).
Score:1
cn flag

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.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.