Problem/Motivation

The path alias field description says, "Use a relative path without a trailing slash" because doing the contrary of either of those things will result in a non-functional alias, but the field validation doesn't check for them.

Proposed resolution

We should check for them. :) Obviously we know they're problems. We may as well prevent them!

(Follow-up to address the duplication that was already in the code before this issue here: #2203573: Path alias validation and tests duplicative, inconsistent.)

Remaining tasks

  • Needs committer review.

User interface changes

None.

API changes

None.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

TravisCarden’s picture

Assigned: TravisCarden » Unassigned
Status: Active » Needs review
FileSize
835 bytes
iaine’s picture

FileSize
1.1 KB

I could not get the error messages to appear until I moved the patch in the path.admin.inc file. After that the error messages showed correctly in the UI.

Status: Needs review » Needs work

The last submitted patch, 1847174-2.patch, failed testing.

TravisCarden’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
FileSize
2.22 KB
1.4 KB

If you move the code there it won't validate taxonomy term aliases. I think my patch was actually correct. All you have to do to test it is to try to create a node or taxonomy term with a path alias that begins or ends with a forward slash (/). Here's the patch again with tests.

iaine’s picture

I see what you are getting at with the taxonomy which your patch fixes and mine missed. However setting the admin form for a url alias allows both leading and trailing slashes as that form appears to run through the path_admin_form functions. I've rerolled with both sets of patches and this outputs the error messages to the UI for the user in both cases.

Status: Needs review » Needs work

The last submitted patch, Path-Alias-Validate-1847174-5.patch, failed testing.

TravisCarden’s picture

Status: Needs work » Needs review
FileSize
3.49 KB
1.4 KB

Ohhh. I was using the node edit form, and you were using the Path admin form, and they don't use the same validation routine. Well, that's ugly. There's duplication (and variation) of logic, different error messages for the same things, and inconsistent test coverage. I don't know if we should resolve the duplication here or file another issue for it. Why don't we get a working patch so we can get a core developer's attention on it and they can make a recommendation. Attached.

@iaine, I'm not certain why your patches are failing, but I notice they contain tabs and trailing spaces. See our Coding standards for information on how we handle indentation and whitespace. In general, if you see red highlighting in git diff or Dreditor, there's a problem. Also, you can run the automated tests yourself on your local installation using the Testing module so you know the outcome before uploading.

TravisCarden’s picture

Rerolling #7. Failing -tests.patch still applies.

Status: Needs review » Needs work

The last submitted patch, 8: path-alias-validation-1847174-8-complete.patch, failed testing.

TravisCarden’s picture

Issue summary: View changes
TravisCarden’s picture

Oh; API changes. Let's try this again.

Note: Added #2203573: Path alias validation and tests duplicative, inconsistent as follow-up to fix API inconsistency.

The last submitted patch, 11: path-alias-validation-1847174-11-tests.patch, failed testing.

Mariano’s picture

Hey all, following the issue here. I've added some testing for the admin form in addition to the current submitted testing improvements.

Status: Needs review » Needs work

The last submitted patch, 13: path-alias-validation-1847174-12-tests.patch, failed testing.

The last submitted patch, 13: path-alias-validation-1847174-12-tests.patch, failed testing.

TravisCarden’s picture

Perfect, @Mariano; thanks! That completes test coverage. I think this is ready for committer review now. For their sake, I'm going to attach a tests patch that includes all the tests added in the complete patch so they can see them fail together and review them at once. And to keep the testbot happy, I'm going to re-attach your complete patch as well. If it passes (as it should), I think we can RTBC this.

The last submitted patch, 16: path-alias-validation-1847174-15-tests.patch, failed testing.

TravisCarden’s picture

Status: Needs review » Reviewed & tested by the community

There we go!

TravisCarden’s picture

Issue summary: View changes
webchick’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/path/path.admin.inc
@@ -235,6 +235,15 @@ function path_admin_form_validate($form, &$form_state) {
+  if (drupal_substr($alias, 0, 1) == '/') {
+    form_set_error('alias', $form_state, t('The alias must be a relative path.'));
...
+  if (drupal_substr($alias, -1) == '/') {
+    form_set_error('alias', $form_state, t('The alias must not have a trailing slash.'));

+++ b/core/modules/path/path.module
@@ -143,6 +144,15 @@ function path_form_element_validate($element, &$form_state, $complete_form) {
+    if (drupal_substr($alias, 0, 1) == '/') {
+      form_error($element, $form_state, t('The alias must be a relative path.'));
...
+    if (drupal_substr($alias, -1) == '/') {
+      form_error($element, $form_state, t('The alias must not have a trailing slash.'));

We're trying not to introduce anymore calls to derepcated procedural wrappers; this should be changed to Unicode::substr(), \Drupal::formBuilder()>setError()/getError().

TravisCarden’s picture

Status: Needs work » Needs review
FileSize
5.3 KB
2.45 KB

We can fix that. Thanks, @webchick! (Tests-only patch is unchanged.)

Mariano’s picture

Status: Needs review » Reviewed & tested by the community

Looks good!

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 21: path-alias-validation-1847174-21-complete.patch, failed testing.

TravisCarden’s picture

TravisCarden’s picture

Status: Needs work » Reviewed & tested by the community

I take it there was a regression in HEAD. This patch never changed, obviously, and it passes again.

webchick’s picture

Status: Reviewed & tested by the community » Needs review

I don't mind committing this, since it's test coverage of an existing code path that previously lacked it, and that's +++.

However, from a usability POV, it would be *way* nicer if Drupal would just strip the trailing slash for you if you happened to provide it. It's like forms that ask you for a phone number and then yell at you when you have "-" in them. I could remove it, but a computer could also remove it just as easily.

Is there a reason not to just do that here, for better UX?

antonioG4’s picture

I'm going to take a stab at rerolling this since the directory structured changed. But also, I'm agreeing with @webchick, that it is annoying to have to reenter something that the validation could also fix, and this is a pretty simplistic replacing of unnecessary "/"

Jody Lynn’s picture

FileSize
6.26 KB

I redid the patch to trim leading and trailing slashed in aliases, to test that they are removed, and to reduce the complexity of the help text.

Status: Needs review » Needs work

The last submitted patch, 28: 1847174-28.patch, failed testing.

Jody Lynn’s picture

FileSize
6.27 KB
Jody Lynn’s picture

Status: Needs work » Needs review
Jody Lynn’s picture

FileSize
6.27 KB

Status: Needs review » Needs work

The last submitted patch, 32: 1847174-29.patch, failed testing.

The last submitted patch, 30: 1847174-29.patch, failed testing.

Status: Needs work » Needs review

Désiré queued 32: 1847174-29.patch for re-testing.

Status: Needs review » Needs work

The last submitted patch, 32: 1847174-29.patch, failed testing.

droplet’s picture

It should trims both "\" & "/"

boromino’s picture

Assigned: Unassigned » boromino

Fixed function name and array index.

boromino’s picture

FileSize
6.3 KB
2.1 KB
boromino’s picture

Status: Needs work » Needs review
znerol’s picture

+++ b/core/modules/path/src/Form/PathFormBase.php
@@ -104,7 +104,7 @@ public function buildForm(array $form, FormStateInterface $form_state, $pid = NU
+      '#description' => $this->t('Specify an alternative path by which this data can be accessed. For example, type "about" when writing an about page. Use a relative path.'),

+++ b/core/modules/path/src/Plugin/Field/FieldWidget/PathWidget.php
@@ -58,7 +58,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
+      '#description' => $this->t('The alternative URL for this content. Use a relative path. For example, enter "about" for the about page.'),

It is weird that in one place the description is about data and in the other it is about content (I realize that this is nothing new, but still might be worth unifying on this occasion). Also would it be possible to use exact the same sentence (less work for translators)?

One thing which is also not clear from the description is that its possible to use slashes in order to build up content hierarchies (reflected in breadcrumbs).

TravisCarden’s picture

FileSize
2.9 KB
6.32 KB
586 bytes

A couple of small issues:

  • trim() can do the whole job in a single invocation. This trims all whitespace characters and both kinds of slashes. $alias = trim($alias, " \t\n\r\0\x0B\\/");
  • There was a little typo in one of the tests, using the wrong node.

Attached is an updated patch that addresses these things and @droplet's comment in #37. I don't happen to have a working installation of D8 at my disposal at the moment, so I can't test (and thus can't RTBC). Hopefully I didn't break it. :)

TravisCarden’s picture

FileSize
2.64 KB

Oops. I didn't get the whole interdiff. Here's the full one.

The last submitted patch, 42: path_alias_validation-1847174-42-tests.patch, failed testing.

znerol’s picture

@TravisCarden: Do you think it is worth addressing #41 or are there any reasons against?

TravisCarden’s picture

My personal feeling is that the two contexts are different enough that it's actually appropriate to have the messages different. You'll notice that they're even written in difference voices. (One is in the imperative mood. The other one isn't technically even a sentence at the beginning--it's a subject clause with a period on the end.) That's not to say that I necessarily like them, but I wouldn't hold up this issue on improving them.

If someone wanted to enhance the "about" alias examples to imply directory depth, I would support that.

znerol’s picture

trim() can do the whole job in a single invocation. This trims all whitespace characters and both kinds of slashes.

 $alias = trim($alias, " \t\n\r\0\x0B\\/"); 

I feel this is much less readable than trim(trim($alias), " \\/");. Other than that the patch is ready.

I leave it to the person working on the patch and the committer on whether or not #41 should be addressed.

TravisCarden’s picture

@znerol: If a literal space is the only whitespace character we want to support on the inside of a slash, then the two are functionally equivalent, and yours is indeed more readable. If we want to strip other whitespace characters (like vertical tab or newline) inside slashes, they are different:

Raw input " / alias/with/slashes-and-whitespace / "
trim($alias, " \t\n\r\0\x0B\\/") "alias/with/slashes-and-whitespace"
trim(trim($alias), " \\/") " alias/with/slashes-and-whitespace "

That contingency seems like an edge case, though--perhaps one that shouldn't be supported. Attached is a patch implementing your suggestion, @znerol. The tests-only patch in #42 remains valid.

znerol’s picture

Status: Needs review » Reviewed & tested by the community

@TravisCarden: I did not realize this ramifications.

Note to committers: This was RTBC twice before (see #20 and #26). All of the concerns expressed there have been addressed.

There was a short discussion starting at #41 on whether or not to reword the description. #47 and #48 are about whether to use one trim or two of them (this is a question about readability vs catching edge cases).

Regardless I think this is ready.

tstoeckler’s picture

This is probably not a problem, because linking to such things does not really make sense, but wanted to note that this will disallow linking to relative URIs such as //example.com/foo.js.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

This issue is a normal bug fix, and doesn't include any disruptive changes, so it is allowed per https://www.drupal.org/core/beta-changes. Committed 79d21e7 and pushed to 8.0.x. Thanks!

  • alexpott committed 79d21e7 on 8.0.x
    Issue #1847174 by TravisCarden, Jody Lynn, boromino, Mariano, iaine:...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

osopolar’s picture