Problem/Motivation

i can't save a cloned page with automatic url alias
MSG: The alias /xyz is already used in this language

Steps to reproduce

- create a page with Auto-Path (Configure URL alias pattern.)
- clone and save the page

Comments

HeavyStoneHead created an issue. See original summary.

smustgrave’s picture

I just used the module and didn't run into this issue?

Sheenam khunger’s picture

I can also reproduce this issue. But in my case, it is only happening when url-alias pattern is like this: /staticword/node-title/
When the url-alias pattern is :/node-title then it is working fine.

neslee canil pinto’s picture

This seems to be a content releated issue, not able to reproduce but with #3 able to reproduce

duaelfr’s picture

Title: Can't save clone with Auto-Path » Can't save clone with Pathauto
Version: 8.x-1.14 » 8.x-1.x-dev

I also have this issue on one of my projects.
After xdebugging this, I figured out that the issue happens in \Drupal\path\Plugin\Field\FieldWidget\PathWidget::validateFormElement() where Drupal checks that the given alias doesn't already exists for another source but the same language.

During the usual process, either the alias is empty (new node) or the alias matches the source (node edit).
In our case, the alias is set to the cloned one by \Drupal\quick_node_clone\Entity\QuickNodeCloneEntityFormBuilder::getForm() but the source is empty so it doesn't match.

I can see two ways of solving this:

  1. we unset the path alias before creating the new node or before building the form
  2. we call \Drupal\pathauto\PathautoGenerator::updateEntityAlias() to generate a new alias before building the form this is not working because it needs a saved entity

Ideally, we should unset the path alias only if the pathauto checkbox is checked but we don't know that before building the form unless we look into the Request which seem really dirty.

FIY the following hook implementation fixes the issues but it's not really clean either:

/**
 * Implements hook_cloned_node_alter().
 */
function MY_MODULE_cloned_node_alter(NodeInterface &$node) {
  $node->path->alias = NULL;
}

Does someone here with a big brain have an idea to fix this issue?