You are here

class Images in Bamboo Twig 8

Provides a 'Images' Twig Extensions.

Hierarchy

  • class \Drupal\bamboo_twig_images\TwigExtension\Images extends \Drupal\bamboo_twig_images\TwigExtension\Twig_Extension

Expanded class hierarchy of Images

1 string reference to 'Images'
bamboo_twig_images.services.yml in bamboo_twig_images/bamboo_twig_images.services.yml
bamboo_twig_images/bamboo_twig_images.services.yml
1 service uses Images
bamboo_twig_images.twig.images in bamboo_twig_images/bamboo_twig_images.services.yml
Drupal\bamboo_twig_images\TwigExtension\Images

File

bamboo_twig_images/src/TwigExtension/Images.php, line 14

Namespace

Drupal\bamboo_twig_images\TwigExtension
View source
class Images extends \Twig_Extension {

  /**
   * Provides a factory for image objects.
   *
   * @var Drupal\Core\Image\ImageFactory
   */
  private $imageFactory;

  /**
   * ImageStyleGenerator Service.
   *
   * @var Drupal\disrupt_tools\Service\ImageStyleGenerator
   */
  private $imageStyleGenerator;

  /**
   * TwigExtension constructor.
   */
  public function __construct(ImageFactory $imageFactory, ImageStyleGenerator $imageStyleGenerator) {
    $this->imageFactory = $imageFactory;
    $this->imageStyleGenerator = $imageStyleGenerator;
  }

  /**
   * List of all Twig functions.
   */
  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',
      ]),
    ];
  }

  /**
   * Unique identifier for this Twig extension.
   */
  public function getName() {
    return 'bamboo_twig.twig.images';
  }

  /**
   * Generate Image Style, with responsive format.
   *
   * @param Drupal\file\Plugin\Field\FieldType\FileFieldItemList $field
   *   Field File Entity to retrieve cover and generate it.
   * @param array $styles
   *   Styles to be generated.
   *
   * @return array
   *   Generated link of styles
   */
  public function imageStyleField(FileFieldItemList $field, array $styles) {
    return $this->imageStyleGenerator
      ->fromField($field, $styles);
  }

  /**
   * Generate Image Style, with responsive format.
   *
   * @param int $fid
   *   File id to generate.
   * @param array $styles
   *   Styles to be generated.
   *
   * @return array
   *   Generated link of styles
   */
  public function imageStyleFile($fid, array $styles) {
    return $this->imageStyleGenerator
      ->fromFile($fid, $styles);
  }

  /**
   * Load an image from file uri.
   *
   * @param string $file_uri
   *   File URI on the current server.
   *
   * @return \Drupal\Core\Image\ImageInterface
   *   An Image object.
   */
  public function getImage($file_uri) {
    return $this->imageFactory
      ->get($file_uri);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Images::$imageFactory private property Provides a factory for image objects.
Images::$imageStyleGenerator private property ImageStyleGenerator Service.
Images::getFunctions public function List of all Twig functions.
Images::getImage public function Load an image from file uri.
Images::getName public function Unique identifier for this Twig extension.
Images::imageStyleField public function Generate Image Style, with responsive format.
Images::imageStyleFile public function Generate Image Style, with responsive format.
Images::__construct public function TwigExtension constructor.