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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

nghai created an issue. See original summary.

nghai’s picture

Issue summary: View changes
Jehu’s picture

same problem here

AmolB’s picture

Facing same problem.

Romisha’s picture

Facing the same issue. Has anyone found any solution to this?

HongPong’s picture

Any 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?

armyofda12mnkeys’s picture

I 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? :

$url = '/node/'. $node->id();
    return new RedirectResponse($url); 

Seems to work for me. Will update if doesn't

nghai’s picture

@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);

armyofda12mnkeys’s picture

Hey @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?

nghai’s picture

Hello @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.

hiramanpatil’s picture

Getting 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)

zepner’s picture

In 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.

kszarek’s picture

Version: 8.x-1.0 » 8.x-1.1
Status: Active » Needs review
FileSize
795 bytes

Hey, introducing patch that fixes this issue

xeM8VfDh’s picture

I'm still seeing this issue on 8.8.4

aguerrerowk’s picture

Same problem in 8.8.4.
Is this problem related to module phone_link?

xeM8VfDh’s picture

@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.

rodrigoaguilera’s picture

Version: 8.x-1.1 » 8.x-1.x-dev
Status: Needs review » Fixed

This is now committed in the project. Thanks!

Status: Fixed » Closed (fixed)

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