6

In order to get entity link, I'm using:

$entity->getTranslation($language)->url();

So it gives me : /en/node/21

But I want this url to be absolute.

How to pass $options = array('absolute' => TRUE); in above node url?

1 Answer 1

8

The options are the second parameter in url(), then you also have to pass the first parameter, which is by default 'canonical':

$entity->getTranslation($language)->url('canonical', ['absolute' => TRUE]);

url() is deprecated, so better use toUrl():

$entity->getTranslation($language)->toUrl('canonical', ['absolute' => TRUE])->toString();
3
  • You can also use ->setAbsolute() which I find more intuitive than figuring out what options can go in the array. so you will end up with $entity->getTranslation($language)->toUrl('canonical', ['absolute' => TRUE])->setAbsolute()->toString();
    – Duncanmoo
    Commented Feb 2, 2018 at 13:46
  • $entity->getTranslation($language)->toUrl('canonical')->setAbsolute()->toString(); You don't need the absolute option in the toUrl() if using ->setAbsolute()
    – diamondsea
    Commented Mar 19, 2018 at 20:01
  • 1
    $entity->getTranslation($language)->toUrl()->setAbsolute()->toString() If you use setAbsolute(), you don't need to pass 'canonical' either.
    – AdamS
    Commented Nov 20, 2018 at 12:06

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.