You are here

public function ToUrl::getUrl in Twig Extender 8.3

Same name and namespace in other branches
  1. 8.4 src/Plugin/TwigPlugin/ToUrl.php \Drupal\twig_extender\Plugin\TwigPlugin\ToUrl::getUrl()
  2. 8.2 src/Plugin/TwigPlugin/ToUrl.php \Drupal\twig_extender\Plugin\TwigPlugin\ToUrl::getUrl()
  3. 4.x src/Plugin/TwigPlugin/ToUrl.php \Drupal\twig_extender\Plugin\TwigPlugin\ToUrl::getUrl()

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 getUrl($url, $absolute = FALSE) {
  if (is_a($url, 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);
}