diff --git a/core/lib/Drupal/Core/Template/TwigExtension.php b/core/lib/Drupal/Core/Template/TwigExtension.php index 0e99830fc5..4783f9dccc 100644 --- a/core/lib/Drupal/Core/Template/TwigExtension.php +++ b/core/lib/Drupal/Core/Template/TwigExtension.php @@ -9,6 +9,7 @@ use Drupal\Core\File\FileUrlGeneratorInterface; use Drupal\Core\Render\AttachmentsInterface; use Drupal\Core\Render\BubbleableMetadata; +use Drupal\Core\Render\Element; use Drupal\Core\Render\Markup; use Drupal\Core\Render\RenderableInterface; use Drupal\Core\Render\RendererInterface; @@ -99,7 +100,7 @@ public function getFunctions() { // This function will receive a renderable array, if an array is detected. new TwigFunction('render_var', [$this, 'renderVar']), // The url and path function are defined in close parallel to those found - // in \Symfony\Bridge\Twig\Extension\RoutingExtension + // in \Symfony\Bridge\Twig\Extension\RoutingExtension. new TwigFunction('url', [$this, 'getUrl'], ['is_safe_callback' => [$this, 'isUrlGenerationSafe']]), new TwigFunction('path', [$this, 'getPath'], ['is_safe_callback' => [$this, 'isUrlGenerationSafe']]), new TwigFunction('link', [$this, 'getLink']), @@ -143,6 +144,9 @@ public function getFilters() { // This filter will render a renderable array to use the string results. new TwigFilter('render', [$this, 'renderVar']), new TwigFilter('format_date', [$this->dateFormatter, 'format']), + // Adding image style filter function. + new TwigFilter('style', [$this, 'getImageStyleUrl']), + new TwigFilter('size', [$this, 'getImageFieldWithStyle']), // Add new theme hook suggestions directly from a Twig template. new TwigFilter('add_suggestion', [$this, 'suggestThemeHook']), ]; @@ -348,7 +352,7 @@ public function isUrlGenerationSafe(Node $args_node) { /** * Attaches an asset library to the template, and hence to the response. * - * Allows Twig templates to attach asset libraries using + * Allows Twig templates to attach asset libraries using. * @code * {{ attach_library('extension/library_name') }} * @endcode @@ -468,7 +472,6 @@ public function escapeFilter(Environment $env, $arg, $strategy = 'html', $charse // This is a normal render array, which is safe by definition, with // special simple cases already handled. - // Early return if this element was pre-rendered (no need to re-render). if (isset($arg['#printed']) && $arg['#printed'] == TRUE && isset($arg['#markup']) && strlen($arg['#markup']) > 0) { return $arg['#markup']; @@ -712,4 +715,47 @@ public function suggestThemeHook(?array $element, string|\Stringable $suggestion return $element; } + /** + * Wrapper around \Drupal\image\ImageStyleInterface::buildUrl(). + * + * @param string $uri + * Url to image. + * @param string $style + * The image style. + * + * @return mixed + * Image url with an image style applied to it. + */ + public function getImageStyleUrl($uri, $style) { + $entity = \Drupal::entityTypeManager()->getStorage('image_style')->load($style); + return $entity->buildUrl($uri); + } + + /** + * Returns a renderable array of an image field with a given image style. + * + * @param array $field + * Renderable array of a field. + * @param string $style + * An image style. + * + * @return mixed + * a renderable array or NULL if there is no valid input. + */ + public function getImageFieldWithStyle(array $field, $style) { + if (!isset($field['#field_type']) || $field['#field_type'] !== 'image') { + return NULL; + } + + $element_children = Element::children($field, TRUE); + + if (!empty($element_children)) { + foreach ($element_children as $key) { + $field[$key]['#image_style'] = $style; + } + } + + return $field; + } + }