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.
| Comment | File | Size | Author |
|---|---|---|---|
| #2 | path-2919657-path_element_language_on_create.patch | 1.17 KB | MaskOta |
Comments
Comment #2
MaskOta commentedI don't know if this is the right aproach but it seems to fix the problem
Comment #3
simo.k commentedThis patch seems to fix the problem.
Comment #6
laravz commentedI'm not quite sure whether there might not be a better approach, however do have some remarks about the patch itself.
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.
Dependency Injection should be used.
Comment #13
nuwans commentedThanks 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!