You are here

public function BlazyEntityReferenceBase::buildStage in Blazy 8

Build the main background/stage, image or video.

Main image can be separate image item from video thumbnail for highres. Fallback to default thumbnail if any, which has no file API.

1 call to BlazyEntityReferenceBase::buildStage()
BlazyEntityReferenceBase::buildElement in src/Dejavu/BlazyEntityReferenceBase.php
Returns item contents.

File

src/Dejavu/BlazyEntityReferenceBase.php, line 162

Class

BlazyEntityReferenceBase
Base class for entity reference formatters with field details.

Namespace

Drupal\blazy\Dejavu

Code

public function buildStage(array &$element, $entity, $langcode) {
  $settings =& $element['settings'];
  $stage = empty($settings['source_field']) ? '' : $settings['source_field'];
  $stage = empty($settings['image']) ? $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.
  if ($stage && isset($entity->{$stage})) {

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

    // Do not proceed if it is a Media entity video.
    if (isset($value[0]) && $value[0]) {

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

          /** @var \Drupal\image\Plugin\Field\FieldType\ImageItem $item */
          $element['item'] = $file
            ->get(0);

          // Collects cache tags to be added for each item in the field.
          $settings['file_tags'] = $file
            ->referencedEntities()[0]
            ->getCacheTags();
          $settings['uri'] = $file
            ->referencedEntities()[0]
            ->getFileUri();
        }
      }
      elseif (isset($value[0]['value']) || isset($value[0]['uri'])) {
        $settings['input_url'] = $this
          ->getFieldString($entity, $stage, $langcode);
        if ($settings['input_url']) {
          $this
            ->buildVideo($settings);
          $element['item'] = $value;
        }
      }
    }
  }
}