You are here

public function BlazyEntityReferenceBase::buildElement in Blazy 7

Build individual item contents.

Overrides BlazyEntityBase::buildElement

File

src/Plugin/Field/FieldFormatter/BlazyEntityReferenceBase.php, line 62

Class

BlazyEntityReferenceBase
Base class for entity reference formatters with field details.

Namespace

Drupal\blazy\Plugin\Field\FieldFormatter

Code

public function buildElement(array &$build, $entity, $delta = 0) {
  $settings =& $build['settings'];
  $item_id = $settings['item_id'] = empty($settings['item_id']) ? 'box' : $settings['item_id'];
  if (!empty($settings['vanilla'])) {
    return parent::buildElement($build, $entity);
  }
  $element = [
    'settings' => $settings,
  ];

  // Built early before stage to allow custom highres video thumbnail later.
  // Implementor must import Drupal\blazy\Dejavu\BlazyVideoTrait.
  // Build the main stage.
  if (!empty($settings['image'])) {
    if (method_exists($this, 'getMediaItem') && isset($entity
      ->wrapper()->{$settings['image']})) {
      $file = (object) $entity
        ->wrapper()->{$settings['image']}
        ->value();
      if ($file && ($image = $this
        ->getImageItem($file))) {
        $element['item'] = $image['item'];
        $element['settings'] = array_merge($settings, $image['settings']);
      }
      $this
        ->getMediaItem($element, $file);
    }

    // If Image rendered is picked, render image as is, a rare case.
    if (!empty($settings['media_switch']) && $settings['media_switch'] == 'rendered') {
      $content = $this
        ->blazyEntity()
        ->getFieldRenderable($entity, $settings['image'], $settings, TRUE);
    }
  }

  // Optional image with responsive image, lazyLoad, and lightbox supports.
  $element[$item_id] = isset($content) ? $content : $this
    ->formatter()
    ->getBlazy($element);

  // Captions if so configured.
  $this
    ->getCaption($element, $entity);

  // Layouts can be builtin, or field, if so configured.
  if (!empty($settings['layout']) && isset($entity->{$settings['layout']})) {
    if (strpos($settings['layout'], 'field_') !== FALSE) {
      $settings['layout'] = $this
        ->blazyEntity()
        ->getFieldString($entity, $settings['layout'], $settings);
    }
    $element['settings']['layout'] = $settings['layout'];
  }

  // Classes, if so configured.
  if (!empty($settings['class']) && isset($entity->{$settings['class']})) {
    $element['settings']['class'] = $this
      ->blazyEntity()
      ->getFieldString($entity, $settings['class'], $settings);
  }

  // Build the main item.
  $build['items'][$delta] = $element;

  // Build the thumbnail item.
  if (!empty($settings['nav'])) {

    // Thumbnail usages: asNavFor pagers, dot, arrows, photobox thumbnails.
    $element[$item_id] = empty($settings['thumbnail_style']) ? [] : $this
      ->formatter()
      ->getThumbnail($element['settings'], $element['item']);
    $element['caption'] = empty($settings['thumbnail_caption']) ? [] : $this
      ->blazyEntity()
      ->getFieldRenderable($entity, $settings['thumbnail_caption'], $settings);
    $build['thumb']['items'][$delta] = $element;
  }
}