2

When I send a node as a simplenews newsletter I use the node's Email: HTML display. One of the fields in this view is a label of a referenced entity to be rendered as a Link to the referenced entity. Drupal renders it as a link with a relative / site-local URL. I want it to be rendered as an absolute / external URL.

For what it's worth:
In the render array the link is stored in
$variable['items']['0']['content']['#url'] which is a Drupal\Core\Url with a protected external property of false

I can't see anything in the Url interface that would change external to true or indeed whether this is the thing to do.

2 Answers 2

5

external and absolute are different:

external is an url without internal route. You can't change this property. This is set when you create an url.

absolute is an option, which you can set at any time in the url object:

$url->setAbsolute();

Or set to false:

$url->setAbsolute(FALSE);

And this will change how the url is generated absolute or relative.

2
  • Totally brilliant, thanks. I was expecting to find such a method in the API documentation but it defeated me .... although I now have found it. I do feel we can do better with the API documentation .... not sure how.
    – iainH
    Commented Apr 8, 2016 at 21:57
  • could you provide said link iain? Commented Jan 9, 2017 at 10:26
2

looks like that's a read-only property. Most other things can be changed with setOption() or specific methods. Not sure if that's an oversight or by design.

You need to build a new Url object object from the node object and pass the option in:

$node->toUrl('canonical', ['external' => TRUE]);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.