3

Pathauto can create path aliases for the node page. For example, it can change node/[nid] to project/[nid], where project is the content type. It doesn't change the path for the node edit page, which is still node/[nid]/edit, instead of project/[nid]/edit as I would expect it to be.

How could I set the path alias to project/[nid]/edit for the node edit page in Drupal 8, when the content type is project?

6
  • Can you describe why you want to change node edit url? because all CRUD operation have some specific route provided by their module. You don't need to alter them. Commented Dec 13, 2018 at 7:44
  • 1
    Simple answer: you can't (with a module or config, this would require custom coding)
    – Hudri
    Commented Dec 13, 2018 at 7:55
  • @Hudri – So, actually you can. The question is how.
    – leymannx
    Commented Dec 13, 2018 at 8:06
  • 1
    Take a look at this tutorial. This adds an alternative path to edit routes, but it does not replace the existing ones
    – Hudri
    Commented Dec 13, 2018 at 8:20
  • 1
    This is not possible by routing. You need an outbound path processor to make links content type aware, see drupal.stackexchange.com/questions/259326/…, in your case inbound too, see drupal.stackexchange.com/questions/213855/… and finally Global Redirect now merged into Redirect.
    – 4uk4
    Commented Dec 14, 2018 at 7:48

2 Answers 2

2

There is a module for that: Sub-Pathauto

From the module page:

The Drupal Path module matches only full URLs when creating SEO-friendly aliases. This module extends that behavior by also matching known sub-paths and replacing them with their respective alias. For example, if user/1 is aliased to users/admin, this module rewrites the link to the user contact page user/1/contact to use the aliased URL users/admin/contact instead.

1

Use a src/Routing/RouteSubscriber.php file in your module.

namespace Drupal\example\Routing;

use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;

/**
 * Listens to the dynamic route events.
 */
class RouteSubscriber extends RouteSubscriberBase {

  /**
   * {@inheritdoc}
   */
  protected function alterRoutes(RouteCollection $collection) {
    if ($route = $collection->get('entity.node.edit_form')) {
      $route->setPath('/project/{node}/edit');
    }
  }

}

Use a example.services.yml file in your module (if the module is named example).

services:
  example.route_subscriber:
    class: Drupal\example\Routing\RouteSubscriber
    tags:
      - { name: event_subscriber }

https://www.drupal.org/docs/8/api/routing-system/altering-existing-routes-and-adding-new-routes-based-on-dynamic-ones

3
  • 2
    Yes, the proposal works! But it applies to all content types and nodes. Suppose there are two content types: project and investor, could I achieve both 'project/nid/edit' and 'investor/nid/edit' ? Thanks a lot!
    – Basic
    Commented Dec 13, 2018 at 14:54
  • I spent my morning attempting to figure out how to set the edit path differently for each individual content type. Unfortunately I came up short and compromised on using a single path that all of my content types would use (e.g. "/my_posts/edit/{node}". I'd love to know if anyone else out there knows how to do this. Commented Oct 25, 2019 at 16:26
  • What described by @4k4 in his comment is the way I would go for it.
    – apaderno
    Commented Jun 10, 2021 at 20:52

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.