I want to use a link field's uri component as the redirect value for a content type. I put [node:field_link:uri] as the value in the Redirect path field of the Rabbit Hole settings for the content type. When I save, this error message happens: "Internal path '[node:field_link:uri]' must begin with a '/', '?', or '#'."

In FormManglerService, validateFormRedirect() is checking if the link is not external, but this check is failing.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

xenophyle created an issue. See original summary.

lehler’s picture

plousia’s picture

Seconded, having the same issue. This would be a really useful functionality which appears to be available but doesn't function as expected.

gr8tkicks’s picture

Yes same here, I have the redirect URL in another field, I add the token which has the full path in the original field and I get the following error:

Internal path '[node:field_external_url:uri]' must begin with a '/', '?', or '#'.

lehler’s picture

(Accidentally double-clicked on captcha when editing an earlier comment to add a new expected behavior and my comment was removed entirely. Reposting in full. Sorry for the confusion.)

Steps to reproduce

  1. Install Drupal with standard profile.
  2. Install and enable both Rabbit Hole and Token.
  3. Add a link field called "Link" to the Article content type using all default settings.
  4. Make the link field required.
  5. Edit the content type.
  6. In the Rabbit Hole settings, choose Page Redirect and set the Redirect Path to [node:field_link:uri].

Expected behavior

The edit form will recognize the redirect path as a Token value (since it says "You may enter tokens in this field.") and save the provided input. Any attempt to view an Article by any user without permissions to bypass Rabbit Hole is redirected to the URI specified in the value of the Link field.

If a different token were used - such as [node:title] for some reason - the form would save successfully and any Article being viewed would attempt to parse the value provided by the token at run time and redirect to a valid URI if used as the title or attempt (and most likely fail) to redirect to an internal path.

Valid non-node tokens - such as [site:url] - could be used as well. The provided example would act as an alias for <front>.

If an invalid token were used - such as [node:field_which_does_not_exist] - the form would fail to save with an error message indicating an invalid token was provided.

Observed behavior

The edit form fails to save with the error message, "Internal path '[node:field_link:uri]' must begin with a '/', '?', or '#'. "

Use case

Content which is hosted outside of Drupal can be used as related content on the site. The node serves as a placeholder which can be used in views, menu items, search, etc.

nord102’s picture

I have created a patch that modifies the FormManglerService.php - validateFormRedirect function to include '[' as an acceptable internal character when the Token module is enabled.

The patch modifies the method of checking the first character of the redirect, but I think this is much cleaner / more understandable method, especially to cleanly incorporate the Token character.

nord102’s picture

Status: Active » Needs review
plousia’s picture

Tested patch in #7 and it works just fine for redirects to the URI of a file field on the content, but token for a link field URL results in "The website encountered an unexpected error. Please try again later." if the link is an internal link beginning with /, an autocomplete-selected entity reference to internal content, or the <front> token. Full external or internal path works.

biz123’s picture

I tried the patch in #7 and it fixed the validation issues in the form for configuring Rabbit Hole on the node type, but I was still getting the error message in #9. I noticed there was markup in the redirect url taken from the token for a link field. Based on the documentation in for `\Drupal\Core\Utility\Token::replace` I used `PlainTextOutput::renderFromHtml()` to render the token as plaintext and that fixed it for me. Attached is the patch from #7 with my updates.

lehler’s picture

Patch #10 seems to be working as expected for me, with just cursory testing so far.

kbentham’s picture

#10 also works for me.

plousia’s picture

Unfortunately #10 fails for me still in the same way reported in #9. For those for whom it's working, I'd be interested to know if you've tested using the [node:field_link:uri] token and a hard-coded internal link beginning with /, a reference to internal content, or the <front> token?

biz123’s picture

plousia, With patch #10 I was able to use a link field token as [node:field_link] and that worked for internal links, external links and <front>, but I do still get errors if I use a token for the uri, [node:field_link:uri].

plousia’s picture

@biz123 ah fantastic!! That's worked for me, thanks!

mandclu’s picture

Status: Needs review » Reviewed & tested by the community

It seems like it would be more intuitive if it could also work work with `[node:field_link:uri]` but I can verify that the patch works if the redirect is configured as described in #14

geoffreyr’s picture

There's a couple of things I noticed about Patch #10 and how it works with URLs derived from tokens pointing to Link fields.

Firstly, if you enter a token which doesn't produce a value, it emits the following error: InvalidArgumentException: The user-entered string '' must begin with a '/', '?', or '#'. in Drupal\Core\Url::fromUserInput() (line 204 of /core/lib/Drupal/Core/Url.php).
This makes sense, and there's no way to define fallback behaviour in Rabbit Hole for pages that don't have a value for this field. (I personally feel it should default to a 404 Not Found, but I understand why that might not work for everyone.)

Also, depending on how the Link field is set up, and how the token is written, it might pull through the truncated URL, which obviously won't work as intended.

Otherwise, this patch is going to be really useful. Presumably the first problem could be avoided if we defined a default value for the Link field, perhaps pointing to an error page or something similar.

amani.mansour’s picture

With patch 10, using this token [node:field_link] did not work for me. Using this token [node:field_link:uri] worked with just external links. So I made a small update to patch 10 so that using this token [node:field_link:uri] would work with both internal and external links.

Anybody’s picture

#18: Validation should also consider this related issue: #2926929: Fatal Error on empty Redirect Path

mmbk’s picture

+1 RTBC Validation of a token does not fail any more

Unfortunately this conflicts with #2926929 To resolve the conflicts I guess it is necessary to apply either of them.

thejimbirch’s picture

The patch in #18 works for [node:field_link] and [node:field_link:uri]

However, it breaks when there is no URL in that field. That may be covered in other issues mentioned above.

benjifisher’s picture

Status: Reviewed & tested by the community » Needs work
FileSize
552 bytes
2.87 KB

I disagree with Comments #18 and #21. In my testing, the patch from #18 (confusingly named ...-11.patch) works with the token [node:field_link:uri], but it does not work with the token [node:field_link].

There is a more serious problem. If I enter something like /node/130 in the Rabbit Hole config (no token) then the patch from #18 leads to an error when I try to visit a page with a redirect:

InvalidArgumentException: The URI '/node/130' is invalid. You must use a valid URI scheme. in Drupal\Core\Url::fromUri() (line 282 of core/lib/Drupal/Core/Url.php).

This means that the patch from #18 would break existing sites that have such redirects.

Based on this, I think we should use the patch from #10 instead of the patch from #18. I am setting the status to NW to do that, with a small change: add something to help text to suggest the correct token to use.

Another problem with the patch from #18 is that it is not properly formed. It has some empty lines, but every line in a patch (except for the metadata lines) should start with " " (context), "-" (removed), or "+" (added). This does not confuse git apply, but it does confuse the interdiff utility.

I am uploading a cleaned-up and renamed version of the patch from #18 and an interdiff comparing it to the patch from #10.

benjifisher’s picture

Status: Needs work » Needs review
FileSize
42.89 KB
915 bytes
3.22 KB

The attached patch does what I suggested in my previous comment. Here is a screenshot showing the revised help text:

field_link] or /my/view?page=[node:field_page_number].'

Because of the problems with the patch in #18, I am hiding it. I have attached my recommended patch and an interdiff comparing it to the one from #10.

JeroenT’s picture

Status: Needs review » Reviewed & tested by the community

+1 for #23. Tried the patch and it was working for my use case "[node:field_link]".

Dylan Donkersgoed’s picture

Status: Reviewed & tested by the community » Fixed

Thank you nord102, biz123 and benjifisher for the patch and all of you for testing. I have merged this in.

Status: Fixed » Closed (fixed)

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

attisan’s picture

Priority: Normal » Major

breaks redirects to when token modules is installed due to

$target = PlainTextOutput::renderFromHtml($target);

Should be en-capsuled in a check to see if the target changed after token replace ran.