Problem/Motivation

The provided argument in my case already includes the leading slash and cannot be modified on the client side. The code assumes that there is no leading slash, so the path argument ends up as //path/alias

Proposed resolution

Check the provided argument and static segment for a slash before initializing the $alias variable.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

drewble created an issue. See original summary.

drewble’s picture

I've rolled a patch against 8.x-1.1 that does the following.

API changes

• Add a private method to check both the provided static segment and the provided argument for a leading slash before assignment to a variable, providing one if a leading slash is not present.

• Initialize the $alias variable with the value of the argument and prepend the static segment, if provided.

heddn’s picture

This might be an easier solution.

heddn’s picture

Status: Active » Needs review
drewble’s picture

@heddn I don't know that #3 solves the problem for an argument with a leading slash.

Say I provide a static segment path.

After the first condition, the value of $alias is path/ at line 115.

If my argument has a leading slash, the next mutation of $alias at line 120 changes the value to path//argument.

The final mutation at line 121 changes $alias to /path//argument

#3 would solve the issue for a provided segment with a leading slash as the last mutation would remove the leading slash from the provided segment.

You could get away with $alias .= '/' . ltrim($argument, '/'); at line 120 and deleting line 121. If you wanted to make the handling for the provided segment more robust, you could give the same treatment to line 117 $alias .= ltrim($this->options['segments'], '/') . '/';

I still like the fewer mutations of $alias in the patch in #2, but I can see the sense in your approach.

Here's a patch with my suggested changes to your approach.

heddn’s picture

+++ b/src/Plugin/views/argument_validator/UrlPath.php
@@ -112,12 +112,12 @@ class UrlPath extends ArgumentValidatorPluginBase {
+      $alias .= ltrim($this->options['segments'], '/') . '/';

Don't you have control over the segments in the view setup? Why can't that be fixed? If we need to, we could (should?) add validation that the segments don't have a leading slash.

Actually, here's a patch that adds form validation. If that doesn't make sense, please be patient with me and explain more of your use case and why the segments must include leading/trailing slashes.

drewble’s picture

You're right. The segment value is totally controlled. I do like the addition of validation. I apologize if I came across as impatient.

With the validation, line 127 can drop the ltrim()

Here's a new patch combining the two.

heddn’s picture

Is #7 needed at all? I don't think it is.

drewble’s picture

I thought it made sense for the convenience of anyone who has the same issue down the line, so they could add a single patch. If it is clutter I suppose it can be removed.

  • heddn committed f8a3f5c on 8.x-1.x authored by drewble
    Issue #3133621 by drewble, heddn: Check provided argument for leading...
heddn’s picture

Status: Needs review » Fixed

The warning message for the form already said not to prefix or suffix with a slash. Now we do proper validation. I think that's probably good enough. Landed this. Thanks for noticing and bringing it up. And posting the first patches.

Status: Fixed » Closed (fixed)

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