|
|
@ -26,13 +26,16 @@ use Drupal\image\Entity\ImageStyle; |
|
|
|
use Drupal\media\MediaInterface; |
|
|
|
use Drupal\media\MediaInterface; |
|
|
|
use Drupal\media\Plugin\media\Source\OEmbedInterface; |
|
|
|
use Drupal\media\Plugin\media\Source\OEmbedInterface; |
|
|
|
use Symfony\Cmf\Component\Routing\RouteObjectInterface; |
|
|
|
use Symfony\Cmf\Component\Routing\RouteObjectInterface; |
|
|
|
|
|
|
|
use Twig\Extension\AbstractExtension; |
|
|
|
|
|
|
|
use Twig\TwigFilter; |
|
|
|
|
|
|
|
use Twig\TwigFunction; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Twig extension with some useful functions and filters. |
|
|
|
* Twig extension with some useful functions and filters. |
|
|
|
* |
|
|
|
* |
|
|
|
* Dependencies are not injected for performance reason. |
|
|
|
* Dependencies are not injected for performance reason. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
class TwigExtension extends \Twig_Extension { |
|
|
|
class TwigExtension extends AbstractExtension { |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* {@inheritdoc} |
|
|
|
* {@inheritdoc} |
|
|
@ -47,14 +50,14 @@ class TwigExtension extends \Twig_Extension { |
|
|
|
// @code |
|
|
|
// @code |
|
|
|
// {{ drupal_view('who_s_new', 'block_1') }} |
|
|
|
// {{ drupal_view('who_s_new', 'block_1') }} |
|
|
|
// @endcode |
|
|
|
// @endcode |
|
|
|
new \Twig_SimpleFunction('drupal_view', 'views_embed_view'), |
|
|
|
new TwigFunction('drupal_view', 'views_embed_view'), |
|
|
|
|
|
|
|
|
|
|
|
// - Drupal View Result - |
|
|
|
// - Drupal View Result - |
|
|
|
// |
|
|
|
// |
|
|
|
// @code |
|
|
|
// @code |
|
|
|
// {{ drupal_view('who_s_new', 'block_1') }} |
|
|
|
// {{ drupal_view('who_s_new', 'block_1') }} |
|
|
|
// @endcode |
|
|
|
// @endcode |
|
|
|
new \Twig_SimpleFunction('drupal_view_result', 'views_get_view_result'), |
|
|
|
new TwigFunction('drupal_view_result', 'views_get_view_result'), |
|
|
|
|
|
|
|
|
|
|
|
// - Drupal Block - |
|
|
|
// - Drupal Block - |
|
|
|
// |
|
|
|
// |
|
|
@ -76,7 +79,7 @@ class TwigExtension extends \Twig_Extension { |
|
|
|
// @endcode |
|
|
|
// @endcode |
|
|
|
// |
|
|
|
// |
|
|
|
// @see https://www.drupal.org/node/2964457#block-plugin |
|
|
|
// @see https://www.drupal.org/node/2964457#block-plugin |
|
|
|
new \Twig_SimpleFunction('drupal_block', [$this, 'drupalBlock']), |
|
|
|
new TwigFunction('drupal_block', [$this, 'drupalBlock']), |
|
|
|
|
|
|
|
|
|
|
|
// - Drupal Region - |
|
|
|
// - Drupal Region - |
|
|
|
// |
|
|
|
// |
|
|
@ -87,7 +90,7 @@ class TwigExtension extends \Twig_Extension { |
|
|
|
// {# Print 'Sidebar First' region of Bartik theme. #} |
|
|
|
// {# Print 'Sidebar First' region of Bartik theme. #} |
|
|
|
// {{ drupal_region('sidebar_first', 'bartik') }} |
|
|
|
// {{ drupal_region('sidebar_first', 'bartik') }} |
|
|
|
// @endcode |
|
|
|
// @endcode |
|
|
|
new \Twig_SimpleFunction('drupal_region', [$this, 'drupalRegion']), |
|
|
|
new TwigFunction('drupal_region', [$this, 'drupalRegion']), |
|
|
|
|
|
|
|
|
|
|
|
// - Drupal Entity - |
|
|
|
// - Drupal Entity - |
|
|
|
// |
|
|
|
// |
|
|
@ -102,7 +105,7 @@ class TwigExtension extends \Twig_Extension { |
|
|
|
// {# admin/structure/block page. #} |
|
|
|
// {# admin/structure/block page. #} |
|
|
|
// {{ drupal_entity('block', 'bartik_branding', check_access=false) }} |
|
|
|
// {{ drupal_entity('block', 'bartik_branding', check_access=false) }} |
|
|
|
// @endcode |
|
|
|
// @endcode |
|
|
|
new \Twig_SimpleFunction('drupal_entity', [$this, 'drupalEntity']), |
|
|
|
new TwigFunction('drupal_entity', [$this, 'drupalEntity']), |
|
|
|
|
|
|
|
|
|
|
|
// - Drupal Entity Form - |
|
|
|
// - Drupal Entity Form - |
|
|
|
// |
|
|
|
// |
|
|
@ -116,7 +119,7 @@ class TwigExtension extends \Twig_Extension { |
|
|
|
// {# Print user register form. #} |
|
|
|
// {# Print user register form. #} |
|
|
|
// {{ drupal_entity_form('user', NULL, 'register', check_access=false) }} |
|
|
|
// {{ drupal_entity_form('user', NULL, 'register', check_access=false) }} |
|
|
|
// @endcode |
|
|
|
// @endcode |
|
|
|
new \Twig_SimpleFunction('drupal_entity_form', [$this, 'drupalEntityForm']), |
|
|
|
new TwigFunction('drupal_entity_form', [$this, 'drupalEntityForm']), |
|
|
|
|
|
|
|
|
|
|
|
// - Drupal Field - |
|
|
|
// - Drupal Field - |
|
|
|
// |
|
|
|
// |
|
|
@ -125,21 +128,21 @@ class TwigExtension extends \Twig_Extension { |
|
|
|
// {{ drupal_field('field_image', 'node', 1, 'teaser') }} |
|
|
|
// {{ drupal_field('field_image', 'node', 1, 'teaser') }} |
|
|
|
// {{ drupal_field('field_image', 'node', 1, {type: 'image_url', settings: {image_style: 'large'}}) }} |
|
|
|
// {{ drupal_field('field_image', 'node', 1, {type: 'image_url', settings: {image_style: 'large'}}) }} |
|
|
|
// @endcode |
|
|
|
// @endcode |
|
|
|
new \Twig_SimpleFunction('drupal_field', [$this, 'drupalField']), |
|
|
|
new TwigFunction('drupal_field', [$this, 'drupalField']), |
|
|
|
|
|
|
|
|
|
|
|
// - Drupal Menu - |
|
|
|
// - Drupal Menu - |
|
|
|
// |
|
|
|
// |
|
|
|
// @code |
|
|
|
// @code |
|
|
|
// {{ drupal_menu('main') }} |
|
|
|
// {{ drupal_menu('main') }} |
|
|
|
// @endcode |
|
|
|
// @endcode |
|
|
|
new \Twig_SimpleFunction('drupal_menu', [$this, 'drupalMenu']), |
|
|
|
new TwigFunction('drupal_menu', [$this, 'drupalMenu']), |
|
|
|
|
|
|
|
|
|
|
|
// - Drupal Form - |
|
|
|
// - Drupal Form - |
|
|
|
// |
|
|
|
// |
|
|
|
// @code |
|
|
|
// @code |
|
|
|
// {{ drupal_form('Drupal\\search\\Form\\SearchBlockForm') }} |
|
|
|
// {{ drupal_form('Drupal\\search\\Form\\SearchBlockForm') }} |
|
|
|
// @endcode |
|
|
|
// @endcode |
|
|
|
new \Twig_SimpleFunction('drupal_form', [$this, 'drupalForm']), |
|
|
|
new TwigFunction('drupal_form', [$this, 'drupalForm']), |
|
|
|
|
|
|
|
|
|
|
|
// - Drupal Image - |
|
|
|
// - Drupal Image - |
|
|
|
// |
|
|
|
// |
|
|
@ -159,21 +162,21 @@ class TwigExtension extends \Twig_Extension { |
|
|
|
// {# Render responsive image. #} |
|
|
|
// {# Render responsive image. #} |
|
|
|
// {{ drupal_image('public://ocean.jpg', 'wide', responsive=true) }} |
|
|
|
// {{ drupal_image('public://ocean.jpg', 'wide', responsive=true) }} |
|
|
|
// @endcode |
|
|
|
// @endcode |
|
|
|
new \Twig_SimpleFunction('drupal_image', [$this, 'drupalImage']), |
|
|
|
new TwigFunction('drupal_image', [$this, 'drupalImage']), |
|
|
|
|
|
|
|
|
|
|
|
// - Drupal Token - |
|
|
|
// - Drupal Token - |
|
|
|
// |
|
|
|
// |
|
|
|
// @code |
|
|
|
// @code |
|
|
|
// {{ drupal_token('site:name') }} |
|
|
|
// {{ drupal_token('site:name') }} |
|
|
|
// @endcode |
|
|
|
// @endcode |
|
|
|
new \Twig_SimpleFunction('drupal_token', [$this, 'drupalToken']), |
|
|
|
new TwigFunction('drupal_token', [$this, 'drupalToken']), |
|
|
|
|
|
|
|
|
|
|
|
// - Drupal Config - |
|
|
|
// - Drupal Config - |
|
|
|
// |
|
|
|
// |
|
|
|
// @code |
|
|
|
// @code |
|
|
|
// {{ drupal_config('system.site', 'name') }} |
|
|
|
// {{ drupal_config('system.site', 'name') }} |
|
|
|
// @endcode |
|
|
|
// @endcode |
|
|
|
new \Twig_SimpleFunction('drupal_config', [$this, 'drupalConfig']), |
|
|
|
new TwigFunction('drupal_config', [$this, 'drupalConfig']), |
|
|
|
|
|
|
|
|
|
|
|
// - Drupal Dump - |
|
|
|
// - Drupal Dump - |
|
|
|
// |
|
|
|
// |
|
|
@ -187,11 +190,11 @@ class TwigExtension extends \Twig_Extension { |
|
|
|
// {# Dump all available variables for the current template. #} |
|
|
|
// {# Dump all available variables for the current template. #} |
|
|
|
// {{ dd() }} |
|
|
|
// {{ dd() }} |
|
|
|
// @endcode |
|
|
|
// @endcode |
|
|
|
new \Twig_SimpleFunction('drupal_dump', [$this, 'drupalDump'], $context_options), |
|
|
|
new TwigFunction('drupal_dump', [$this, 'drupalDump'], $context_options), |
|
|
|
new \Twig_SimpleFunction('dd', [$this, 'drupalDump'], $context_options), |
|
|
|
new TwigFunction('dd', [$this, 'drupalDump'], $context_options), |
|
|
|
|
|
|
|
|
|
|
|
// - Drupal Title - |
|
|
|
// - Drupal Title - |
|
|
|
new \Twig_SimpleFunction('drupal_title', [$this, 'drupalTitle']), |
|
|
|
new TwigFunction('drupal_title', [$this, 'drupalTitle']), |
|
|
|
|
|
|
|
|
|
|
|
// - Drupal URL - |
|
|
|
// - Drupal URL - |
|
|
|
// |
|
|
|
// |
|
|
@ -202,7 +205,7 @@ class TwigExtension extends \Twig_Extension { |
|
|
|
// {# Complex URL. #} |
|
|
|
// {# Complex URL. #} |
|
|
|
// {{ drupal_url('node/1', {query: {foo: 'bar'}, fragment: 'example', absolute: true}) }} |
|
|
|
// {{ drupal_url('node/1', {query: {foo: 'bar'}, fragment: 'example', absolute: true}) }} |
|
|
|
// @endcode |
|
|
|
// @endcode |
|
|
|
new \Twig_SimpleFunction('drupal_url', [$this, 'drupalUrl']), |
|
|
|
new TwigFunction('drupal_url', [$this, 'drupalUrl']), |
|
|
|
|
|
|
|
|
|
|
|
// - Drupal Link - |
|
|
|
// - Drupal Link - |
|
|
|
// |
|
|
|
// |
|
|
@ -213,16 +216,16 @@ class TwigExtension extends \Twig_Extension { |
|
|
|
// {# This link will only be shown for privileged users. #} |
|
|
|
// {# This link will only be shown for privileged users. #} |
|
|
|
// {{ drupal_link('Example'|t, '/admin', check_access=true) }} |
|
|
|
// {{ drupal_link('Example'|t, '/admin', check_access=true) }} |
|
|
|
// @endcode |
|
|
|
// @endcode |
|
|
|
new \Twig_SimpleFunction('drupal_link', [$this, 'drupalLink']), |
|
|
|
new TwigFunction('drupal_link', [$this, 'drupalLink']), |
|
|
|
|
|
|
|
|
|
|
|
// - Drupal Messages - |
|
|
|
// - Drupal Messages - |
|
|
|
new \Twig_SimpleFunction('drupal_messages', [$this, 'drupalMessages']), |
|
|
|
new TwigFunction('drupal_messages', [$this, 'drupalMessages']), |
|
|
|
|
|
|
|
|
|
|
|
// - Drupal Breadcrumb - |
|
|
|
// - Drupal Breadcrumb - |
|
|
|
new \Twig_SimpleFunction('drupal_breadcrumb', [$this, 'drupalBreadcrumb']), |
|
|
|
new TwigFunction('drupal_breadcrumb', [$this, 'drupalBreadcrumb']), |
|
|
|
|
|
|
|
|
|
|
|
// - Drupal Breakpoint - |
|
|
|
// - Drupal Breakpoint - |
|
|
|
new \Twig_SimpleFunction('drupal_breakpoint', [$this, 'drupalBreakpoint'], $all_options), |
|
|
|
new TwigFunction('drupal_breakpoint', [$this, 'drupalBreakpoint'], $all_options), |
|
|
|
|
|
|
|
|
|
|
|
// - Contextual Links - |
|
|
|
// - Contextual Links - |
|
|
|
// |
|
|
|
// |
|
|
@ -238,7 +241,7 @@ class TwigExtension extends \Twig_Extension { |
|
|
|
// {{ content }} |
|
|
|
// {{ content }} |
|
|
|
// </div> |
|
|
|
// </div> |
|
|
|
// @endcode |
|
|
|
// @endcode |
|
|
|
new \Twig_SimpleFunction('contextual_links', [$this, 'contextualLinks']), |
|
|
|
new TwigFunction('contextual_links', [$this, 'contextualLinks']), |
|
|
|
]; |
|
|
|
]; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -260,7 +263,7 @@ class TwigExtension extends \Twig_Extension { |
|
|
|
// <div>[site:slogan]</div> |
|
|
|
// <div>[site:slogan]</div> |
|
|
|
// {% endapply %} |
|
|
|
// {% endapply %} |
|
|
|
// @endcode |
|
|
|
// @endcode |
|
|
|
new \Twig_SimpleFilter('token_replace', [$this, 'tokenReplaceFilter']), |
|
|
|
new TwigFilter('token_replace', [$this, 'tokenReplaceFilter']), |
|
|
|
|
|
|
|
|
|
|
|
// - Preg Replace - |
|
|
|
// - Preg Replace - |
|
|
|
// |
|
|
|
// |
|
|
@ -270,42 +273,42 @@ class TwigExtension extends \Twig_Extension { |
|
|
|
// |
|
|
|
// |
|
|
|
// For simple string interpolation consider using built-in 'replace' or |
|
|
|
// For simple string interpolation consider using built-in 'replace' or |
|
|
|
// 'format' Twig filters. |
|
|
|
// 'format' Twig filters. |
|
|
|
new \Twig_SimpleFilter('preg_replace', [$this, 'pregReplaceFilter']), |
|
|
|
new TwigFilter('preg_replace', [$this, 'pregReplaceFilter']), |
|
|
|
|
|
|
|
|
|
|
|
// - Image Style - |
|
|
|
// - Image Style - |
|
|
|
// |
|
|
|
// |
|
|
|
// @code |
|
|
|
// @code |
|
|
|
// {{ 'public://images/ocean.jpg'|image_style('thumbnail') }} |
|
|
|
// {{ 'public://images/ocean.jpg'|image_style('thumbnail') }} |
|
|
|
// @endcode |
|
|
|
// @endcode |
|
|
|
new \Twig_SimpleFilter('image_style', [$this, 'imageStyle']), |
|
|
|
new TwigFilter('image_style', [$this, 'imageStyle']), |
|
|
|
|
|
|
|
|
|
|
|
// - Transliterate - |
|
|
|
// - Transliterate - |
|
|
|
// |
|
|
|
// |
|
|
|
// @code |
|
|
|
// @code |
|
|
|
// {{ 'Привет!'|transliterate }} |
|
|
|
// {{ 'Привет!'|transliterate }} |
|
|
|
// @endcode |
|
|
|
// @endcode |
|
|
|
new \Twig_SimpleFilter('transliterate', [$this, 'transliterate']), |
|
|
|
new TwigFilter('transliterate', [$this, 'transliterate']), |
|
|
|
|
|
|
|
|
|
|
|
// - Check Markup - |
|
|
|
// - Check Markup - |
|
|
|
// |
|
|
|
// |
|
|
|
// @code |
|
|
|
// @code |
|
|
|
// {{ '<b>bold</b> <strong>strong</strong>'|check_markup('restricted_html') }} |
|
|
|
// {{ '<b>bold</b> <strong>strong</strong>'|check_markup('restricted_html') }} |
|
|
|
// @endcode |
|
|
|
// @endcode |
|
|
|
new \Twig_SimpleFilter('check_markup', [$this, 'checkMarkup']), |
|
|
|
new TwigFilter('check_markup', [$this, 'checkMarkup']), |
|
|
|
|
|
|
|
|
|
|
|
// - Format Size - |
|
|
|
// - Format Size - |
|
|
|
// |
|
|
|
// |
|
|
|
// @code |
|
|
|
// @code |
|
|
|
// {{ 12345|format_size() }} |
|
|
|
// {{ 12345|format_size() }} |
|
|
|
// @endcode |
|
|
|
// @endcode |
|
|
|
new \Twig_SimpleFilter('format_size', 'format_size'), |
|
|
|
new TwigFilter('format_size', 'format_size'), |
|
|
|
|
|
|
|
|
|
|
|
// - Truncate - |
|
|
|
// - Truncate - |
|
|
|
// |
|
|
|
// |
|
|
|
// @code |
|
|
|
// @code |
|
|
|
// {{ 'Some long text'|truncate(10, true) }} |
|
|
|
// {{ 'Some long text'|truncate(10, true) }} |
|
|
|
// @endcode |
|
|
|
// @endcode |
|
|
|
new \Twig_SimpleFilter('truncate', [$this, 'truncate']), |
|
|
|
new TwigFilter('truncate', [$this, 'truncate']), |
|
|
|
|
|
|
|
|
|
|
|
// - View - |
|
|
|
// - View - |
|
|
|
// |
|
|
|
// |
|
|
@ -319,7 +322,7 @@ class TwigExtension extends \Twig_Extension { |
|
|
|
// {{ node.field_image|view('teaser') }} |
|
|
|
// {{ node.field_image|view('teaser') }} |
|
|
|
// {{ node.field_image|view({settings: {image_style: 'thumbnail'}}) }} |
|
|
|
// {{ node.field_image|view({settings: {image_style: 'thumbnail'}}) }} |
|
|
|
// @endcode |
|
|
|
// @endcode |
|
|
|
new \Twig_SimpleFilter('view', [$this, 'view']), |
|
|
|
new TwigFilter('view', [$this, 'view']), |
|
|
|
|
|
|
|
|
|
|
|
// - With - |
|
|
|
// - With - |
|
|
|
// |
|
|
|
// |
|
|
@ -330,7 +333,7 @@ class TwigExtension extends \Twig_Extension { |
|
|
|
// {# Set nested value. #} |
|
|
|
// {# Set nested value. #} |
|
|
|
// {{ content|with(['field_image', '#title'], 'Photo'|t) }} |
|
|
|
// {{ content|with(['field_image', '#title'], 'Photo'|t) }} |
|
|
|
// @endcode |
|
|
|
// @endcode |
|
|
|
new \Twig_SimpleFilter('with', [$this, 'with']), |
|
|
|
new TwigFilter('with', [$this, 'with']), |
|
|
|
|
|
|
|
|
|
|
|
// - Children - |
|
|
|
// - Children - |
|
|
|
// |
|
|
|
// |
|
|
@ -341,7 +344,7 @@ class TwigExtension extends \Twig_Extension { |
|
|
|
// {% endfor %} |
|
|
|
// {% endfor %} |
|
|
|
// </ul> |
|
|
|
// </ul> |
|
|
|
// @endcode |
|
|
|
// @endcode |
|
|
|
new \Twig_SimpleFilter('children', [$this, 'children']), |
|
|
|
new TwigFilter('children', [$this, 'children']), |
|
|
|
|
|
|
|
|
|
|
|
// - File URI - |
|
|
|
// - File URI - |
|
|
|
// |
|
|
|
// |
|
|
@ -368,7 +371,7 @@ class TwigExtension extends \Twig_Extension { |
|
|
|
// {{ media_uri|image_style('thumbnail') }} |
|
|
|
// {{ media_uri|image_style('thumbnail') }} |
|
|
|
// {% endif %} |
|
|
|
// {% endif %} |
|
|
|
// @endcode |
|
|
|
// @endcode |
|
|
|
new \Twig_SimpleFilter('file_uri', [$this, 'fileUri']), |
|
|
|
new TwigFilter('file_uri', [$this, 'fileUri']), |
|
|
|
|
|
|
|
|
|
|
|
// - File URL - |
|
|
|
// - File URL - |
|
|
|
// |
|
|
|
// |
|
|
@ -389,7 +392,7 @@ class TwigExtension extends \Twig_Extension { |
|
|
|
// @code |
|
|
|
// @code |
|
|
|
// {{ node.field_media|file_url }} |
|
|
|
// {{ node.field_media|file_url }} |
|
|
|
// @endcode |
|
|
|
// @endcode |
|
|
|
new \Twig_SimpleFilter('file_url', [$this, 'fileUrl']), |
|
|
|
new TwigFilter('file_url', [$this, 'fileUrl']), |
|
|
|
]; |
|
|
|
]; |
|
|
|
|
|
|
|
|
|
|
|
if (Settings::get('twig_tweak_enable_php_filter')) { |
|
|
|
if (Settings::get('twig_tweak_enable_php_filter')) { |
|
|
@ -412,7 +415,7 @@ class TwigExtension extends \Twig_Extension { |
|
|
|
// @code |
|
|
|
// @code |
|
|
|
// {{ 'now'|date('Y') }} |
|
|
|
// {{ 'now'|date('Y') }} |
|
|
|
// @endcode |
|
|
|
// @endcode |
|
|
|
$filters[] = new \Twig_SimpleFilter('php', [$this, 'phpFilter'], ['needs_context' => TRUE]); |
|
|
|
$filters[] = new TwigFilter('php', [$this, 'phpFilter'], ['needs_context' => TRUE]); |
|
|
|
} |
|
|
|
} |
|
|
|
return $filters; |
|
|
|
return $filters; |
|
|
|
} |
|
|
|
} |
|
|
|