When I create a new node and assign it a URL alias, the langcode of the form element is not set. This causes the validateFormElement to not validate the existence of the alias, and I can accidentally add an alias which already exists. When I edit an existing node, the langcode is set correctly and the validateFormElement sets the error "The alias is already in use."

The langcode value is set in the PathWidget.php line 50:

    $element['langcode'] = [
      '#type' => 'value',
      '#value' => $items[$delta]->langcode,
    ];

$items list seems to be empty when creating a new node, so the langcode is set to null.

Repeatable: Always
Steps to repeat:
1. Create a basic page
2. Assign a URL alias /testpage in URL Path Settings
3. Save
4. Create another basic page
5. Assign a URL alias /testpage in URL Path Settings
6. Save

Expected result:
User sees an error message "The alias is already in use."

Actual result:
The node is successfully saved and a duplicate alias is created.

Comments

simo.k created an issue. See original summary.

MaskOta’s picture

Version: 8.4.0 » 8.5.x-dev
Status: Active » Needs review
StatusFileSize
new1.17 KB

I don't know if this is the right aproach but it seems to fix the problem

simo.k’s picture

This patch seems to fix the problem.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

laravz’s picture

Status: Needs review » Needs work

I'm not quite sure whether there might not be a better approach, however do have some remarks about the patch itself.

+++ b/core/modules/path/src/Plugin/Field/FieldWidget/PathWidget.php
@@ -25,6 +25,13 @@ class PathWidget extends WidgetBase {
+      $input = $form_state->getUserInput();

The getUserInput() returns raw and unvalidated form data. Unless we are absolutely sure that no user can alter the langcode value, the data should be validated/sanitized.

+++ b/core/modules/path/src/Plugin/Field/FieldWidget/PathWidget.php
@@ -25,6 +25,13 @@ class PathWidget extends WidgetBase {
+      $langcode = \Drupal::languageManager()->getCurrentLanguage()->getId();

Dependency Injection should be used.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

nuwans’s picture

Status: Needs work » Closed (outdated)

Thanks for reporting this issue.
I was not able to reproduce this issue on the Drupal 9.4-dev

Error message : The alias /testpage is already in use in this language.

If there are different steps to reproduce the issue please add those steps to the issue summary and move the issue back.
Thanks for your work on this issue!