Browse Source

Added image style filter.

8.x-1.x
Chi 8 years ago
parent
commit
91d6b68922
  1. 18
      src/TwigExtension.php

18
src/TwigExtension.php

@ -7,6 +7,7 @@ use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Site\Settings; use Drupal\Core\Site\Settings;
use Drupal\Core\Utility\Token; use Drupal\Core\Utility\Token;
use Drupal\image\Entity\ImageStyle;
/** /**
* Twig extension with some useful functions and filters. * Twig extension with some useful functions and filters.
@ -81,6 +82,7 @@ class TwigExtension extends \Twig_Extension {
$filters = [ $filters = [
new \Twig_SimpleFilter('token_replace', [$this, 'tokenReplaceFilter']), new \Twig_SimpleFilter('token_replace', [$this, 'tokenReplaceFilter']),
new \Twig_SimpleFilter('preg_replace', [$this, 'pregPeplaceFilter']), new \Twig_SimpleFilter('preg_replace', [$this, 'pregPeplaceFilter']),
new \Twig_SimpleFilter('image_style', [$this, 'imageStyle']),
]; ];
// PHP filter should be enabled in settings.php file. // PHP filter should be enabled in settings.php file.
if (Settings::get('twig_tweak_enable_php_filter')) { if (Settings::get('twig_tweak_enable_php_filter')) {
@ -238,4 +240,20 @@ class TwigExtension extends \Twig_Extension {
return preg_replace('/' . preg_quote($pattern, '/') . '/', $replacement, $text); return preg_replace('/' . preg_quote($pattern, '/') . '/', $replacement, $text);
} }
/**
* Returns the URL of this image derivative for an original image path or URI.
*
* @param string $path
* The path or URI to the original image.
* @param string $style
* The image style.
*
* @return string
* The absolute URL where a style image can be downloaded, suitable for use
* in an <img> tag. Requesting the URL will cause the image to be created.
*/
public function imageStyle($path, $style) {
return ImageStyle::load($style)->buildUrl($path);
}
} }

Loading…
Cancel
Save