You are here

public static function BlazyMedia::imageItem in Blazy 8.2

Same name and namespace in other branches
  1. 7 src/BlazyMedia.php \Drupal\blazy\BlazyMedia::imageItem()

Extracts image from non-media entities for the main background/ stage.

Main image can be separate image item from video thumbnail for highres. Fallback to default thumbnail if any, which has no file API. This used to be for non-media File Entity Reference at 1.x, things changed since then. This is currently reused for Paragraphs which embeds Media.

Parameters

array $element: The element array might contain item and settings.

object $entity: The file entity or entityreference which might have image item.

Overrides BlazyMediaInterface::imageItem

See also

\Drupal\blazy\Dejavu\BlazyEntityMediaBase::buildElement

1 call to BlazyMedia::imageItem()
BlazyEntityMediaBase::buildElement in src/Dejavu/BlazyEntityMediaBase.php
Returns item contents.

File

src/BlazyMedia.php, line 159

Class

BlazyMedia
Provides extra utilities to work with core Media.

Namespace

Drupal\blazy

Code

public static function imageItem(array &$data, $entity) {
  $settings =& $data['settings'];
  $stage = $settings['image'];

  // The actual video thumbnail has already been downloaded earlier.
  // This fetches the highres image if provided and available.
  // With a mix of image and video, image is not always there.

  /** @var \Drupal\file\Plugin\Field\FieldType\FileFieldItemList $file */
  if (isset($entity->{$stage}) && ($file = $entity
    ->get($stage))) {
    $value = $file
      ->getValue();

    // Do not proceed if it is a Media entity video. This means File here.
    if (isset($value[0]) && !empty($value[0]['target_id'])) {

      // If image, even if multi-value, we can only have one stage per slide.
      if (method_exists($file, 'referencedEntities') && isset($file
        ->referencedEntities()[0])) {
        $reference = $file
          ->referencedEntities()[0];

        /** @var \Drupal\image\Plugin\Field\FieldType\ImageItem $item */

        /** @var \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem $item */
        $image = $file
          ->first();

        /** @var Drupal\media\Entity\Media $reference */
        if ($reference instanceof Media) {
          self::mediaItem($data, $reference);
        }
        elseif ($image instanceof ImageItem) {
          $data['item'] = $image;

          // Collects cache tags to be added for each item in the field.
          $settings['file_tags'] = $file
            ->referencedEntities()[0]
            ->getCacheTags();
          $settings['uri'] = Blazy::uri($data['item']);
        }
      }
    }
  }
}