ToUrl.php in Twig Extender 8
File
src/Plugin/TwigPlugin/ToUrl.php
View source
<?php
namespace Drupal\twig_extender\Plugin\TwigPlugin;
use Drupal\twig_extender\Plugin\Twig\TwigPluginBase;
use Drupal\Core\Entity;
class ToUrl extends TwigPluginBase {
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);
}
}
Classes
Name |
Description |
ToUrl |
The plugin for render a url string of url object or ContentEntityBase object. |