Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
I am getting below error when I press Publish/Unpublish link on a Node:
InvalidArgumentException: The URI '' is invalid. You must use a valid URI scheme. in Drupal\Core\Url::fromUri() (line 280 of core/lib/Drupal/Core/Url.php).
Drupal\publishcontent\Controller\PublishContentPublishEntity->toggleEntityStatus(Object)
call_user_func_array(Array, Array) (Line: 123)
This action change the publish status of the nodes but does not redirect back to the same node.
Proposed resolution
It looks like this is because of the empty uri which is fetched in Drupal\publishcontent\Controller\PublishContentPublishEntity at line 26:
$redirectUrl = Url::fromUri(\Drupal::request()->server->get('HTTP_REFERER'), ['absolute' => TRUE])->getUri();
via HTTP_REFERER server variable which is not a really be trusted variable.
Comment | File | Size | Author |
---|---|---|---|
#13 | publishcontent-fix-redirect-uri-2824526-13.patch | 795 bytes | kszarek |
Comments
Comment #2
nghai CreditAttribution: nghai commentedComment #3
Jehu CreditAttribution: Jehu commentedsame problem here
Comment #4
AmolB CreditAttribution: AmolB commentedFacing same problem.
Comment #5
Romisha CreditAttribution: Romisha commentedFacing the same issue. Has anyone found any solution to this?
Comment #6
HongPong CreditAttribution: HongPong as a volunteer and at kor group commentedAny link field with invalid URL value will cause this error btw. I submitted an issue proposing a more fault tolerant approach here: #2953330: Url::fromUri() can throw an exception with a bad URI value from user data, log an error instead?
Comment #7
armyofda12mnkeys CreditAttribution: armyofda12mnkeys commentedI also get this issue where Publish/Unpublish bring up this error (it still seems to publish and unpublish, but I have to train our users to go back and refresh the page to make sure their action occurred...).
I'm running a Drupal 8 site on my Platform.sh host (which runs on AWS/Cloudfront).
They said: "This is an effect of Cloudfront. Cloudfront is paranoid about its use of headers/cookies and so only passes those headers and cookies which are specifically whitelisted and so considered part of the cache key. This is done to prevent cache issues where cached pages are shown to the wrong user by being based on variables not in the cache key.
The HTTP_REFERER header cannot be used with Cloudfront without effectively disabling the caching provided. If necessary it can be added, but as it becomes part of the cache key it will ruin the sites cacheability."
Is there a solution within the module for this? or do we have to wait for a fix in Drupal Core (like in /web/core/lib/Drupal/Core/Url.php )
Maybe we can just do a simple redirect instead in Publishcontent/src/Controller/PublishContentPublishEntity.php? :
Seems to work for me. Will update if doesn't
Comment #8
nghai CreditAttribution: nghai commented@armyofda12mnkeys
I also agreed that HTTP_REFERER is empty sometimes so the implementation can be turn simpler like this:
$url = Url::fromRoute('entity.node.canonical', ['node' => $node->id()], $options);
Comment #9
armyofda12mnkeys CreditAttribution: armyofda12mnkeys commentedHey @nghai,
Thanks for posting...
just curious, I'm passing that $url you posted to the RedirectResponse but getting a:
Recoverable fatal error: Object of class Drupal\Core\Url could not be converted to string in Drupal\Component\Utility\UrlHelper::isExternal() (line 219 of core/lib/Drupal/Component/Utility/UrlHelper.php).
Does it not know the node url is internal? Note: i dont have any $options, its an empty array.
I did see other Drupal functions like Url::fromUri take a string prefixed with 'internal:/node/4' but seems like Url::fromRoute does not like that?
How would i get this working for internal route?
Comment #10
nghai CreditAttribution: nghai commentedHello @armyofda12mnkeys,
Can you try below approach :
$url = Url::fromRoute('entity.node.canonical', ['node' => $node->id()])->toString();
You would also require to include: use Drupal\Core\Url; at the top of the File
$option argument can also be spiked when it is empty.
Let me know if it works else Url::fromUri can also be tried.
Comment #11
hiramanpatil CreditAttribution: hiramanpatil commentedGetting the same error.
InvalidArgumentException: The URI '' is invalid. You must use a valid URI scheme. in Drupal\Core\Url::fromUri() (line 281 of /PROJECT-PATH/core/lib/Drupal/Core/Url.php).
I am getting this error on webform submissions page. (URL - /admin/structure/webform/manage/WEBFORM_NAME/settings)
Comment #12
zepner CreditAttribution: zepner at Aten Design Group commentedIn my case, there were malformed links stored as values within Link fields.
The tables in which the URI's are stored incorrectly may be node__field_link, config_pages__field_link, paragraph__field_links, for example. Also you'll need to correct the values in the corresponding revision tables. Fixing these eliminated the issue.
Comment #13
kszarek CreditAttribution: kszarek commentedHey, introducing patch that fixes this issue
Comment #14
xeM8VfDh CreditAttribution: xeM8VfDh commentedI'm still seeing this issue on 8.8.4
Comment #15
aguerrerowk CreditAttribution: aguerrerowk commentedSame problem in 8.8.4.
Is this problem related to module phone_link?
Comment #16
xeM8VfDh CreditAttribution: xeM8VfDh commented@aguerrerowk, my issue was related to the aggregator module in core. The RSS feed it was pointing at was providing relative paths in the "link" field instead of full/valid URIs. As a result, I had database fields that were of type URI, and their values were not valid URIs. I believe this was throwing the error. Upon dropping those database records, the error went away. So, perhaps your issue is related to your phone_link module, or something else. Whatever the case, it is probably related to some URI field in your database that has non-URI value(s).
This error is vague though, at best.
Comment #18
rodrigoaguileraThis is now committed in the project. Thanks!