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


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?


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();
  • 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 Feb 2 '18 at 13:46
  • $entity->getTranslation($language)->toUrl('canonical')->setAbsolute()->toString(); You don't need the absolute option in the toUrl() if using ->setAbsolute() – diamondsea Mar 19 '18 at 20:01
  • $entity->getTranslation($language)->toUrl()->setAbsolute()->toString() If you use setAbsolute(), you don't need to pass 'canonical' either. – AdamS Nov 20 '18 at 12:06

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.