diff --git a/composer.json b/composer.json index a32559b..3b92f81 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,8 @@ "source": "https://git.drupalcode.org/project/twig_tweak" }, "require": { - "drupal/core": "^8.7 || ^9.0" + "drupal/core": "^8.7 || ^9.0", + "twig/twig": "^1.41 || ^2.12" }, "suggest": { "symfony/var-dumper": "Better dump() function for debugging Twig variables" diff --git a/src/TwigExtension.php b/src/TwigExtension.php index 6ad62a9..0c04dbe 100644 --- a/src/TwigExtension.php +++ b/src/TwigExtension.php @@ -26,13 +26,16 @@ use Drupal\image\Entity\ImageStyle; use Drupal\media\MediaInterface; use Drupal\media\Plugin\media\Source\OEmbedInterface; use Symfony\Cmf\Component\Routing\RouteObjectInterface; +use Twig\Extension\AbstractExtension; +use Twig\TwigFilter; +use Twig\TwigFunction; /** * Twig extension with some useful functions and filters. * * Dependencies are not injected for performance reason. */ -class TwigExtension extends \Twig_Extension { +class TwigExtension extends AbstractExtension { /** * {@inheritdoc} @@ -47,14 +50,14 @@ class TwigExtension extends \Twig_Extension { // @code // {{ drupal_view('who_s_new', 'block_1') }} // @endcode - new \Twig_SimpleFunction('drupal_view', 'views_embed_view'), + new TwigFunction('drupal_view', 'views_embed_view'), // - Drupal View Result - // // @code // {{ drupal_view('who_s_new', 'block_1') }} // @endcode - new \Twig_SimpleFunction('drupal_view_result', 'views_get_view_result'), + new TwigFunction('drupal_view_result', 'views_get_view_result'), // - Drupal Block - // @@ -76,7 +79,7 @@ class TwigExtension extends \Twig_Extension { // @endcode // // @see https://www.drupal.org/node/2964457#block-plugin - new \Twig_SimpleFunction('drupal_block', [$this, 'drupalBlock']), + new TwigFunction('drupal_block', [$this, 'drupalBlock']), // - Drupal Region - // @@ -87,7 +90,7 @@ class TwigExtension extends \Twig_Extension { // {# Print 'Sidebar First' region of Bartik theme. #} // {{ drupal_region('sidebar_first', 'bartik') }} // @endcode - new \Twig_SimpleFunction('drupal_region', [$this, 'drupalRegion']), + new TwigFunction('drupal_region', [$this, 'drupalRegion']), // - Drupal Entity - // @@ -102,7 +105,7 @@ class TwigExtension extends \Twig_Extension { // {# admin/structure/block page. #} // {{ drupal_entity('block', 'bartik_branding', check_access=false) }} // @endcode - new \Twig_SimpleFunction('drupal_entity', [$this, 'drupalEntity']), + new TwigFunction('drupal_entity', [$this, 'drupalEntity']), // - Drupal Entity Form - // @@ -116,7 +119,7 @@ class TwigExtension extends \Twig_Extension { // {# Print user register form. #} // {{ drupal_entity_form('user', NULL, 'register', check_access=false) }} // @endcode - new \Twig_SimpleFunction('drupal_entity_form', [$this, 'drupalEntityForm']), + new TwigFunction('drupal_entity_form', [$this, 'drupalEntityForm']), // - Drupal Field - // @@ -125,21 +128,21 @@ class TwigExtension extends \Twig_Extension { // {{ drupal_field('field_image', 'node', 1, 'teaser') }} // {{ drupal_field('field_image', 'node', 1, {type: 'image_url', settings: {image_style: 'large'}}) }} // @endcode - new \Twig_SimpleFunction('drupal_field', [$this, 'drupalField']), + new TwigFunction('drupal_field', [$this, 'drupalField']), // - Drupal Menu - // // @code // {{ drupal_menu('main') }} // @endcode - new \Twig_SimpleFunction('drupal_menu', [$this, 'drupalMenu']), + new TwigFunction('drupal_menu', [$this, 'drupalMenu']), // - Drupal Form - // // @code // {{ drupal_form('Drupal\\search\\Form\\SearchBlockForm') }} // @endcode - new \Twig_SimpleFunction('drupal_form', [$this, 'drupalForm']), + new TwigFunction('drupal_form', [$this, 'drupalForm']), // - Drupal Image - // @@ -159,21 +162,21 @@ class TwigExtension extends \Twig_Extension { // {# Render responsive image. #} // {{ drupal_image('public://ocean.jpg', 'wide', responsive=true) }} // @endcode - new \Twig_SimpleFunction('drupal_image', [$this, 'drupalImage']), + new TwigFunction('drupal_image', [$this, 'drupalImage']), // - Drupal Token - // // @code // {{ drupal_token('site:name') }} // @endcode - new \Twig_SimpleFunction('drupal_token', [$this, 'drupalToken']), + new TwigFunction('drupal_token', [$this, 'drupalToken']), // - Drupal Config - // // @code // {{ drupal_config('system.site', 'name') }} // @endcode - new \Twig_SimpleFunction('drupal_config', [$this, 'drupalConfig']), + new TwigFunction('drupal_config', [$this, 'drupalConfig']), // - Drupal Dump - // @@ -187,11 +190,11 @@ class TwigExtension extends \Twig_Extension { // {# Dump all available variables for the current template. #} // {{ dd() }} // @endcode - new \Twig_SimpleFunction('drupal_dump', [$this, 'drupalDump'], $context_options), - new \Twig_SimpleFunction('dd', [$this, 'drupalDump'], $context_options), + new TwigFunction('drupal_dump', [$this, 'drupalDump'], $context_options), + new TwigFunction('dd', [$this, 'drupalDump'], $context_options), // - Drupal Title - - new \Twig_SimpleFunction('drupal_title', [$this, 'drupalTitle']), + new TwigFunction('drupal_title', [$this, 'drupalTitle']), // - Drupal URL - // @@ -202,7 +205,7 @@ class TwigExtension extends \Twig_Extension { // {# Complex URL. #} // {{ drupal_url('node/1', {query: {foo: 'bar'}, fragment: 'example', absolute: true}) }} // @endcode - new \Twig_SimpleFunction('drupal_url', [$this, 'drupalUrl']), + new TwigFunction('drupal_url', [$this, 'drupalUrl']), // - Drupal Link - // @@ -213,16 +216,16 @@ class TwigExtension extends \Twig_Extension { // {# This link will only be shown for privileged users. #} // {{ drupal_link('Example'|t, '/admin', check_access=true) }} // @endcode - new \Twig_SimpleFunction('drupal_link', [$this, 'drupalLink']), + new TwigFunction('drupal_link', [$this, 'drupalLink']), // - Drupal Messages - - new \Twig_SimpleFunction('drupal_messages', [$this, 'drupalMessages']), + new TwigFunction('drupal_messages', [$this, 'drupalMessages']), // - Drupal Breadcrumb - - new \Twig_SimpleFunction('drupal_breadcrumb', [$this, 'drupalBreadcrumb']), + new TwigFunction('drupal_breadcrumb', [$this, 'drupalBreadcrumb']), // - Drupal Breakpoint - - new \Twig_SimpleFunction('drupal_breakpoint', [$this, 'drupalBreakpoint'], $all_options), + new TwigFunction('drupal_breakpoint', [$this, 'drupalBreakpoint'], $all_options), // - Contextual Links - // @@ -238,7 +241,7 @@ class TwigExtension extends \Twig_Extension { // {{ content }} // // @endcode - new \Twig_SimpleFunction('contextual_links', [$this, 'contextualLinks']), + new TwigFunction('contextual_links', [$this, 'contextualLinks']), ]; } @@ -260,7 +263,7 @@ class TwigExtension extends \Twig_Extension { //