2

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. Dec 13 '18 at 7:44
  • Simple answer: you can't (with a module or config, this would require custom coding)
    – Hudri
    Dec 13 '18 at 7:55
  • @Hudri – So, actually you can. The question is how.
    – leymannx
    Dec 13 '18 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
    Dec 13 '18 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.
    – 4k4
    Dec 14 '18 at 7:48
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
    Dec 13 '18 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. Oct 25 '19 at 16:26
  • What described by @4k4 in his comment is the way I would go for it.
    – apaderno
    Jun 10 '21 at 20:52
1

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.

Your Answer

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

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