You are here

public function BlazyVideoTrait::getImageItem in Blazy 8

Same name and namespace in other branches
  1. 8.2 src/Dejavu/BlazyVideoTrait.php \Drupal\blazy\Dejavu\BlazyVideoTrait::getImageItem()

Gets the faked image item out of file entity, or ER, if applicable.

Parameters

object $file: The expected file entity, or ER, to get image item from.

Return value

array The array of image item and settings if a file image, else empty.

1 call to BlazyVideoTrait::getImageItem()
BlazyViewsFieldFile::render in src/Plugin/views/field/BlazyViewsFieldFile.php
Renders the field.

File

src/Dejavu/BlazyVideoTrait.php, line 106

Class

BlazyVideoTrait
A Trait common for optional Media Entity and Video Embed Media integration.

Namespace

Drupal\blazy\Dejavu

Code

public function getImageItem($file) {
  $data = [];
  $entity = $file;

  /** @var Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem $file */
  if (isset($file->entity) && !isset($file->alt)) {
    $entity = $file->entity;
  }
  if (!$entity instanceof File) {
    return $data;
  }

  /** @var \Drupal\file\Entity\File $entity */
  list($type, ) = explode('/', $entity
    ->getMimeType(), 2);
  $uri = $entity
    ->getFileUri();
  if ($type == 'image' && ($image = $this
    ->imageFactory()
    ->get($uri)) && $image
    ->isValid()) {
    $item = new \stdClass();
    $item->target_id = $entity
      ->id();
    $item->width = $image
      ->getWidth();
    $item->height = $image
      ->getHeight();
    $item->alt = $entity
      ->getFilename();
    $item->title = $entity
      ->getFilename();
    $item->uri = $uri;
    $settings = (array) $item;
    $item->entity = $entity;
    $settings['type'] = 'image';

    // Build item and settings.
    $data['item'] = $item;
    $data['settings'] = $settings;
    unset($item);
  }
  return $data;
}