1

Pathauto can create URL alias for node view page, for example, URL from node/nid to project/nid (project is a name of content type), but the link to the edit page is not /project/nid/edit, but /node/nid/edit.

How could I create URL alias (like: /project/nid/edit) for node edit page in Drupal 8?

  • 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. – Yogesh Kushwaha 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
  • 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
0

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

  • 1
    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

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.