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.
My use case is I want to give the URL for an entity in an email sent by Drupal:
This code:
$entity->toUrl('canonical', ['absolute' => TRUE])->getUri();
causes an exception to be thrown, with the message 'This URL has a Drupal route, so the canonical form is not a URI'.
This code works:
$entity->toUrl('canonical', ['absolute' => TRUE])->toString();
Why can't getURI() call toString() for a routed URL object?
Comment | File | Size | Author |
---|---|---|---|
#14 | return-uri-string-for-routed-urls-2722303-14.patch | 1.32 KB | Qandeel |
Comments
Comment #5
geekygnr CreditAttribution: geekygnr at GiantGoat Web Development Inc. commentedI ran into this problem as well from a Url object that was generated with the Url::fromRoute function.
It looks like the Url::getUri() function is a straight getter function for the variable on the object. I also found a Url::toUriString() function that worked well.
Not sure what the use case is for having these two functions. I would expect the two functions to return the same thing always making them redundant.
Comment #7
ericmulder1980 CreditAttribution: ericmulder1980 as a volunteer commentedI came here after searching for a problem with
This resulted in the error "This URL has a Drupal route, so the canonical form is not a URI" as stated by the OP.
So when you want to get the Uri (path) for a specific route the code below seems to work.
This returns "/user/login"
This returns "user/login"
Why would Url:fromRoute() not use the urlGenerator to create a full Url object?
Kind regards,
Eric Mulder
Comment #12
Blanca.Esqueda CreditAttribution: Blanca.Esqueda as a volunteer and commentedI'm trying to get the URI schema from an entity.
using: $referenced_entity->toUrl()->getUri();
triggers the error "This URL has a Drupal route, so the canonical form is not a URI".
using: $referenced_entity->toUrl()->toUriString();
returns the route: route:entity.node.canonical;node=16
instead of the uri schema: entity:node/16
I've tried in different ways but it seems like I can not get the URI schema from an entity.
Some help would be appreciated.
Comment #13
Blanca.Esqueda CreditAttribution: Blanca.Esqueda as a volunteer and commentedAs a solution, I propose getUri() to return the URI instead of the error: 'This URL has a Drupal route, so the canonical form is not a URI'
getUri() in https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Url.php/c...
would change to something like this:
Comment #14
Qandeel CreditAttribution: Qandeel as a volunteer commentedInstead of updated getUri(); I would suggest to have a new public function dealing with this new functionality, I have created a patch for this; drupal core 8.9.2