View source
<?php
namespace Drupal\twig_tweak;
use Drupal\Component\Utility\NestedArray;
use Drupal\Component\Utility\Unicode;
use Drupal\Component\Uuid\Uuid;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Link;
use Drupal\Core\Render\Element;
use Drupal\Core\Render\Markup;
use Drupal\Core\Site\Settings;
use Drupal\Core\Theme\ThemeManagerInterface;
use Drupal\Core\Url;
use Drupal\image\Entity\ImageStyle;
use Twig\Environment;
use Twig\Extension\AbstractExtension;
use Twig\Markup as TwigMarkup;
use Twig\TwigFilter;
use Twig\TwigFunction;
class TwigTweakExtension extends AbstractExtension {
protected $moduleHandler;
protected $themeManager;
public function __construct(ModuleHandlerInterface $module_handler, ThemeManagerInterface $theme_manager) {
$this->moduleHandler = $module_handler;
$this->themeManager = $theme_manager;
}
public function getFunctions() : array {
$context_options = [
'needs_context' => TRUE,
];
$all_options = [
'needs_environment' => TRUE,
'needs_context' => TRUE,
];
$functions = [
new TwigFunction('drupal_view', 'views_embed_view'),
new TwigFunction('drupal_view_result', 'views_get_view_result'),
new TwigFunction('drupal_block', [
self::class,
'drupalBlock',
]),
new TwigFunction('drupal_region', [
self::class,
'drupalRegion',
]),
new TwigFunction('drupal_entity', [
self::class,
'drupalEntity',
]),
new TwigFunction('drupal_entity_form', [
self::class,
'drupalEntityForm',
]),
new TwigFunction('drupal_field', [
self::class,
'drupalField',
]),
new TwigFunction('drupal_menu', [
self::class,
'drupalMenu',
]),
new TwigFunction('drupal_form', [
self::class,
'drupalForm',
]),
new TwigFunction('drupal_image', [
self::class,
'drupalImage',
]),
new TwigFunction('drupal_token', [
self::class,
'drupalToken',
]),
new TwigFunction('drupal_config', [
self::class,
'drupalConfig',
]),
new TwigFunction('drupal_dump', [
self::class,
'drupalDump',
], $context_options),
new TwigFunction('dd', [
self::class,
'drupalDump',
], $context_options),
new TwigFunction('drupal_title', [
self::class,
'drupalTitle',
]),
new TwigFunction('drupal_url', [
self::class,
'drupalUrl',
]),
new TwigFunction('drupal_link', [
self::class,
'drupalLink',
]),
new TwigFunction('drupal_messages', function () : array {
return [
'#type' => 'status_messages',
];
}),
new TwigFunction('drupal_breadcrumb', [
self::class,
'drupalBreadcrumb',
]),
new TwigFunction('drupal_breakpoint', [
self::class,
'drupalBreakpoint',
], $all_options),
new TwigFunction('drupal_contextual_links', [
self::class,
'drupalContextualLinks',
]),
];
$this->moduleHandler
->alter('twig_tweak_functions', $functions);
$this->themeManager
->alter('twig_tweak_functions', $functions);
return $functions;
}
public function getFilters() : array {
$filters = [
new TwigFilter('token_replace', [
self::class,
'tokenReplaceFilter',
]),
new TwigFilter('preg_replace', [
self::class,
'pregReplaceFilter',
]),
new TwigFilter('image_style', [
self::class,
'imageStyleFilter',
]),
new TwigFilter('transliterate', [
self::class,
'transliterateFilter',
]),
new TwigFilter('check_markup', 'check_markup'),
new TwigFilter('format_size', 'format_size'),
new TwigFilter('truncate', [
Unicode::class,
'truncate',
]),
new TwigFilter('view', [
self::class,
'viewFilter',
]),
new TwigFilter('with', [
self::class,
'withFilter',
]),
new TwigFilter('children', [
self::class,
'childrenFilter',
]),
new TwigFilter('file_uri', [
self::class,
'fileUriFilter',
]),
new TwigFilter('file_url', [
self::class,
'fileUrlFilter',
]),
new TwigFilter('translation', [
self::class,
'entityTranslation',
]),
new TwigFilter('cache_metadata', [
self::class,
'CacheMetadata',
]),
];
if (Settings::get('twig_tweak_enable_php_filter')) {
$filters[] = new TwigFilter('php', [
self::class,
'phpFilter',
], [
'needs_context' => TRUE,
]);
}
$this->moduleHandler
->alter('twig_tweak_filters', $filters);
$this->themeManager
->alter('twig_tweak_filters', $filters);
return $filters;
}
public function getTests() : array {
$tests = [];
$this->moduleHandler
->alter('twig_tweak_tests', $tests);
$this->themeManager
->alter('twig_tweak_tests', $tests);
return $tests;
}
public static function drupalBlock(string $id, array $configuration = [], bool $wrapper = TRUE) : array {
return \Drupal::service('twig_tweak.block_view_builder')
->build($id, $configuration, $wrapper);
}
public static function drupalRegion(string $region, ?string $theme = NULL) : array {
return \Drupal::service('twig_tweak.region_view_builder')
->build($region, $theme);
}
public static function drupalEntity(string $entity_type, string $selector, string $view_mode = 'full', ?string $langcode = NULL, bool $check_access = TRUE) : array {
$storage = \Drupal::entityTypeManager()
->getStorage($entity_type);
if (Uuid::isValid($selector)) {
$entities = $storage
->loadByProperties([
'uuid' => $selector,
]);
$entity = reset($entities);
}
else {
$entity = $storage
->load($selector);
}
if ($entity) {
return \Drupal::service('twig_tweak.entity_view_builder')
->build($entity, $view_mode, $langcode, $check_access);
}
return [];
}
public static function drupalEntityForm(string $entity_type, ?string $id = NULL, string $form_mode = 'default', array $values = [], bool $check_access = TRUE) : array {
$entity_storage = \Drupal::entityTypeManager()
->getStorage($entity_type);
$entity = $id ? $entity_storage
->load($id) : $entity_storage
->create($values);
if ($entity) {
return \Drupal::service('twig_tweak.entity_form_view_builder')
->build($entity, $form_mode, $check_access);
}
return [];
}
public static function drupalField(string $field_name, string $entity_type, string $id, $view_mode = 'full', string $langcode = NULL, bool $check_access = TRUE) : array {
$entity = \Drupal::entityTypeManager()
->getStorage($entity_type)
->load($id);
if ($entity) {
return \Drupal::service('twig_tweak.field_view_builder')
->build($entity, $field_name, $view_mode, $langcode, $check_access);
}
return [];
}
public static function drupalMenu(string $menu_name, int $level = 1, int $depth = 0, bool $expand = FALSE) : array {
return \Drupal::service('twig_tweak.menu_view_builder')
->build($menu_name, $level, $depth, $expand);
}
public static function drupalForm(string $form_id, ...$args) : array {
$callback = [
\Drupal::formBuilder(),
'getForm',
];
return call_user_func_array($callback, func_get_args());
}
public static function drupalImage(string $selector, string $style = NULL, array $attributes = [], bool $responsive = FALSE, bool $check_access = TRUE) : array {
if (preg_match('/^\\d+$/', $selector)) {
$selector_type = 'fid';
}
elseif (Uuid::isValid($selector)) {
$selector_type = 'uuid';
}
else {
$selector_type = 'uri';
}
$files = \Drupal::entityTypeManager()
->getStorage('file')
->loadByProperties([
$selector_type => $selector,
]);
if (count($files) != 1) {
return [];
}
$file = reset($files);
return \Drupal::service('twig_tweak.image_view_builder')
->build($file, $style, $attributes, $responsive, $check_access);
}
public static function drupalToken(string $token, array $data = [], array $options = []) : string {
return \Drupal::token()
->replace("[{$token}]", $data, $options);
}
public static function drupalConfig(string $name, string $key) {
return \Drupal::config($name)
->get($key);
}
public static function drupalDump(array $context, $variable = NULL) : void {
$var_dumper = '\\Symfony\\Component\\VarDumper\\VarDumper';
if (class_exists($var_dumper)) {
call_user_func($var_dumper . '::dump', func_num_args() == 1 ? $context : $variable);
}
else {
trigger_error('Could not dump the variable because symfony/var-dumper component is not installed.', E_USER_WARNING);
}
}
public static function drupalTitle() : array {
$title = \Drupal::service('title_resolver')
->getTitle(\Drupal::request(), \Drupal::routeMatch()
->getRouteObject());
$build['#markup'] = render($title);
$build['#cache']['contexts'] = [
'url',
];
return $build;
}
public static function drupalUrl(string $user_input, array $options = [], bool $check_access = FALSE) : ?Url {
if (isset($options['langcode'])) {
$language_manager = \Drupal::languageManager();
if ($language = $language_manager
->getLanguage($options['langcode'])) {
$options['language'] = $language;
}
}
if (!in_array($user_input[0], [
'/',
'#',
'?',
])) {
$user_input = '/' . $user_input;
}
$url = Url::fromUserInput($user_input, $options);
return !$check_access || $url
->access() ? $url : NULL;
}
public static function drupalLink($text, string $user_input, array $options = [], bool $check_access = FALSE) : ?Link {
$url = self::drupalUrl($user_input, $options, $check_access);
if ($url) {
if ($text instanceof TwigMarkup) {
$text = Markup::create($text);
}
return Link::fromTextAndUrl($text, $url);
}
return NULL;
}
public static function drupalBreadcrumb() : array {
return \Drupal::service('breadcrumb')
->build(\Drupal::routeMatch())
->toRenderable();
}
public static function drupalContextualLinks(string $id) : array {
$build['#cache']['contexts'] = [
'user.permissions',
];
if (\Drupal::currentUser()
->hasPermission('access contextual links')) {
$build['#type'] = 'contextual_links_placeholder';
$build['#id'] = $id;
}
return $build;
}
public static function drupalBreakpoint(Environment $environment, array $context) : void {
if (function_exists('xdebug_break')) {
xdebug_break();
}
else {
trigger_error('Could not make a break because xdebug is not available.', E_USER_WARNING);
}
}
public static function tokenReplaceFilter(string $text, array $data = [], array $options = []) : string {
return \Drupal::token()
->replace($text, $data, $options);
}
public static function pregReplaceFilter(string $text, string $pattern, string $replacement) : string {
return preg_replace($pattern, $replacement, $text);
}
public static function imageStyleFilter(?string $path, string $style) : ?string {
if (!$path) {
trigger_error('Image path is empty.');
return NULL;
}
if (!($image_style = ImageStyle::load($style))) {
trigger_error(sprintf('Could not load image style %s.', $style));
return NULL;
}
if (!$image_style
->supportsUri($path)) {
trigger_error(sprintf('Could not apply image style %s.', $style));
return NULL;
}
return file_url_transform_relative($image_style
->buildUrl($path));
}
public static function transliterateFilter(string $text, string $langcode = 'en', string $unknown_character = '?', int $max_length = NULL) {
return \Drupal::transliteration()
->transliterate($text, $langcode, $unknown_character, $max_length);
}
public static function viewFilter(?object $object, $view_mode = 'default', string $langcode = NULL, bool $check_access = TRUE) : array {
$build = [];
if ($object instanceof FieldItemListInterface || $object instanceof FieldItemInterface) {
$build = $object
->view($view_mode);
if ($parent = $object
->getParent()) {
CacheableMetadata::createFromRenderArray($build)
->merge(CacheableMetadata::createFromObject($parent
->getEntity()))
->applyTo($build);
}
}
elseif ($object instanceof EntityInterface) {
$build = \Drupal::service('twig_tweak.entity_view_builder')
->build($object, $view_mode, $langcode, $check_access);
}
return $build;
}
public static function withFilter(array $build, $key, $element) : array {
if (is_array($key)) {
NestedArray::setValue($build, $key, $element);
}
else {
$build[$key] = $element;
}
return $build;
}
public static function childrenFilter(array $build, bool $sort = FALSE) : array {
$keys = Element::children($build, $sort);
return array_intersect_key($build, array_flip($keys));
}
public static function fileUriFilter($input) : ?string {
return \Drupal::service('twig_tweak.uri_extractor')
->extractUri($input);
}
public static function fileUrlFilter($input, bool $relative = TRUE) : ?string {
return \Drupal::service('twig_tweak.url_extractor')
->extractUrl($input, $relative);
}
public static function entityTranslation(EntityInterface $entity, string $langcode = NULL) : EntityInterface {
return \Drupal::service('entity.repository')
->getTranslationFromContext($entity, $langcode);
}
public static function cacheMetadata($input) : array {
return \Drupal::service('twig_tweak.cache_metadata_extractor')
->extractCacheMetadata($input);
}
public static function phpFilter(array $context, string $code) {
extract($context);
ob_start();
print eval($code);
$output = ob_get_contents();
ob_end_clean();
return $output;
}
}