Getting a rewritten alias URL canonical entity URL (e.g. /my-alias/for-the-node) as string in modern Drupal is simple:
$entity->toUrl('canonical')->toString();Finding out how to get the "raw" unrewritten URL (e.g. /node/123) wasn't that easy:
$entity->toUrl('canonical', ['path_processing' => FALSE])->toString();Further documentation, especially for further options can be found here:
- https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Url.php/class/Url
- https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Routing!UrlGeneratorInterface.php/function/UrlGeneratorInterface%3A%3AgenerateFromRoute


