diff --git a/core/core.services.yml b/core/core.services.yml index e791572..db65301 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -1444,7 +1444,7 @@ services: - { name: service_collector, tag: 'twig.extension', call: addExtension } twig.extension: class: Drupal\Core\Template\TwigExtension - arguments: ['@renderer'] + arguments: ['@renderer', '@entity.manager'] tags: - { name: twig.extension, priority: 100 } calls: diff --git a/core/includes/theme.inc b/core/includes/theme.inc index a9a8826..9cab7ec 100644 --- a/core/includes/theme.inc +++ b/core/includes/theme.inc @@ -745,6 +745,10 @@ function template_preprocess_links(&$variables) { function template_preprocess_image(&$variables) { if (!empty($variables['uri'])) { $variables['attributes']['src'] = file_create_url($variables['uri']); + + if (empty($variables['original_uri'])) { + $variables['original_uri'] = $variables['uri']; + } } // Generate a srcset attribute conforming to the spec at // http://www.w3.org/html/wg/drafts/html/master/embedded-content.html#attr-img-srcset @@ -1745,7 +1749,7 @@ function drupal_common_theme() { // - http://dev.w3.org/html5/spec/Overview.html#alt // The title attribute is optional in all cases, so it is omitted by // default. - 'variables' => array('uri' => NULL, 'width' => NULL, 'height' => NULL, 'alt' => '', 'title' => NULL, 'attributes' => array(), 'sizes' => NULL, 'srcset' => array(), 'style_name' => NULL), + 'variables' => array('uri' => NULL, 'width' => NULL, 'height' => NULL, 'alt' => '', 'title' => NULL, 'attributes' => array(), 'sizes' => NULL, 'srcset' => array(), 'style_name' => NULL, 'original_uri' => NULL), ), 'breadcrumb' => array( 'variables' => array('links' => array()), diff --git a/core/lib/Drupal/Core/Template/TwigExtension.php b/core/lib/Drupal/Core/Template/TwigExtension.php index 23091d5..3e5c82b 100644 --- a/core/lib/Drupal/Core/Template/TwigExtension.php +++ b/core/lib/Drupal/Core/Template/TwigExtension.php @@ -18,6 +18,7 @@ use Drupal\Core\Datetime\DateFormatter; use Drupal\Core\Render\RenderableInterface; use Drupal\Core\Render\RendererInterface; +use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Routing\UrlGeneratorInterface; use Drupal\Core\Theme\ThemeManagerInterface; use Drupal\Core\Url; @@ -46,6 +47,13 @@ class TwigExtension extends \Twig_Extension { protected $renderer; /** + * The entity manager. + * + * @var \Drupal\Core\Entity\EntityManagerInterface + */ + protected $entityManager; + + /** * The theme manager. * * @var \Drupal\Core\Theme\ThemeManagerInterface @@ -65,8 +73,9 @@ class TwigExtension extends \Twig_Extension { * @param \Drupal\Core\Render\RendererInterface $renderer * The renderer. */ - public function __construct(RendererInterface $renderer) { + public function __construct(RendererInterface $renderer, EntityManagerInterface $entity_manager) { $this->renderer = $renderer; + $this->entityManager = $entity_manager; } /** @@ -173,6 +182,7 @@ public function getFilters() { // This filter will render a renderable array to use the string results. new \Twig_SimpleFilter('render', array($this, 'renderVar')), new \Twig_SimpleFilter('format_date', array($this->dateFormatter, 'format')), + new \Twig_SimpleFilter('style', array($this, 'getImageStyleUrl')), ); } @@ -519,6 +529,23 @@ public function renderVar($arg) { } /** + * Wrapper around \Drupal\image\ImageStyleInterface::buildUrl(). + * + * @param string $uri + * Uri to image. + * @param string $style + * The image style. + * + * @return mixed + * Image url with an image style applied to it. + */ + public function getImageStyleUrl($uri, $style) { + $style = $this->entityManager->getStorage('image_style')->load($style); + + return $style->buildUrl($uri); + } + + /** * Joins several strings together safely. * * @param \Twig_Environment $env diff --git a/core/modules/image/image.module b/core/modules/image/image.module index 2c222f8..952d352 100644 --- a/core/modules/image/image.module +++ b/core/modules/image/image.module @@ -287,6 +287,7 @@ function template_preprocess_image_style(&$variables) { '#attributes' => $variables['attributes'], '#uri' => $style->buildUrl($variables['uri']), '#style_name' => $variables['style_name'], + '#original_uri' => $variables['uri'], ); if (isset($variables['alt']) || array_key_exists('alt', $variables)) {