Images.php in Bamboo Twig 8
File
bamboo_twig_images/src/TwigExtension/Images.php
View source
<?php
namespace Drupal\bamboo_twig_images\TwigExtension;
use Drupal\file\Plugin\Field\FieldType\FileFieldItemList;
use Drupal\Core\Image\ImageFactory;
use Drupal\disrupt_tools\Service\ImageStyleGenerator;
class Images extends \Twig_Extension {
private $imageFactory;
private $imageStyleGenerator;
public function __construct(ImageFactory $imageFactory, ImageStyleGenerator $imageStyleGenerator) {
$this->imageFactory = $imageFactory;
$this->imageStyleGenerator = $imageStyleGenerator;
}
public function getFunctions() {
return [
new \Twig_SimpleFunction('image_style_field', [
$this,
'imageStyleField',
]),
new \Twig_SimpleFunction('image_style_file', [
$this,
'imageStyleFile',
]),
new \Twig_SimpleFunction('get_image', [
$this,
'getImage',
]),
];
}
public function getName() {
return 'bamboo_twig.twig.images';
}
public function imageStyleField(FileFieldItemList $field, array $styles) {
return $this->imageStyleGenerator
->fromField($field, $styles);
}
public function imageStyleFile($fid, array $styles) {
return $this->imageStyleGenerator
->fromFile($fid, $styles);
}
public function getImage($file_uri) {
return $this->imageFactory
->get($file_uri);
}
}
Classes
Name |
Description |
Images |
Provides a 'Images' Twig Extensions. |