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.

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.

|improve this answer|||||
  • 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 Apr 8 '16 at 21:57
  • could you provide said link iain? – autopoietic Jan 9 '17 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]);
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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