0

I have a custom content type which I fill up with the help of the Forms Steps module.

I want to set custom URL for node/add/{custom-content-type}. I tried using a route subscriber and the following code.

/**
 * {@inheritdoc}
 */
protected function alterRoutes(RouteCollection $collection) { 
  if ($route = $collection->get('node.add_page')) {
    $current_uri = \Drupal::request()->getRequestUri();
    if ($current_uri == "node/add/custom-content-type") {
      $route->setPath('/Custom-content/add');
    }
  }
}

The above code is not changing the URL. I am finding difficult to change the URL only for the custom content type.

How can I achieve it?

4
  • 2
    This can be done, but it would be easier to enable the path module and redirect module, and create an alias and a redirect to that alias. – Jaypan Jun 10 at 8:22
  • Does this answer your question? How to create URL alias for node edit page – leymannx Jun 10 at 8:53
  • 2
    Is there any good reason to change from node/add/[content-type] to [content-type]/add for a single content type? The reason why Drupal uses node/add/[content-type] is to avoid conflicts with other routes defined from third-party modules. Also, a placeholder cannot be the first part in the route path; the first part of a route path can only be a static string, like 'node'. – kiamlaluno Jun 10 at 14:06
  • The question doesn't make clear if there is any need to programmatically do what the question describes. I still think it's a bad idea, but if I were to do it, I would do it as @Jaypan described. – kiamlaluno Jun 10 at 20:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.