You are here

public function ToUrl::toUrl in Twig Extender 8

Implementation for render block.

File

src/Plugin/TwigPlugin/ToUrl.php, line 24

Class

ToUrl
The plugin for render a url string of url object or ContentEntityBase object.

Namespace

Drupal\twig_extender\Plugin\TwigPlugin

Code

public function toUrl($url, $absolute = false) {
  if (is_a($url, \Drupal\Core\Entity\ContentEntityBase::class)) {
    $url = $url
      ->toUrl();
  }
  if (is_a($url, 'Drupal\\Core\\Url')) {
    $url = $url
      ->toString();
  }
  if (gettype($url) !== 'string') {
    throw new \Exception('Could not convert object to a path alias');
  }
  return \Drupal::service('path.alias_manager')
    ->getAliasByPath($url);
}