You are here

public function BlazyEntityMediaBase::buildElement in Blazy 8.2

Returns item contents.

Overrides BlazyEntityBase::buildElement

File

src/Dejavu/BlazyEntityMediaBase.php, line 56

Class

BlazyEntityMediaBase
Base class for Media entity reference formatters with field details.

Namespace

Drupal\blazy\Dejavu

Code

public function buildElement(array &$build, $entity, $langcode) {
  $settings =& $build['settings'];
  $item_id = $settings['item_id'];
  $view_mode = $settings['view_mode'] = empty($settings['view_mode']) ? 'full' : $settings['view_mode'];

  // Bail out if vanilla (rendered entity) is required.
  if (!empty($settings['vanilla'])) {
    return parent::buildElement($build, $entity, $langcode);
  }

  // Otherwise hard work which is meant to reduce custom code at theme level.
  $delta = $settings['delta'];
  $element = [
    'settings' => $settings,
  ];

  // Built media item including custom highres video thumbnail.
  $this
    ->blazyOembed()
    ->getMediaItem($element, $entity);

  // Build the main stage with image options from highres video thumbnail.
  if (!empty($settings['image'])) {

    // If Image rendered is picked, render image as is.
    if (!empty($settings['media_switch']) && $settings['media_switch'] == 'rendered') {
      $element['content'][] = $this
        ->blazyEntity()
        ->getFieldRenderable($entity, $settings['image'], $view_mode);
    }
    elseif (empty($element['item']) && empty($settings['uri'])) {
      BlazyMedia::imageItem($element, $entity);
    }
  }

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

  // Optional image with responsive image, lazyLoad, and lightbox supports.
  // Including potential rich Media contents: local video, Facebook, etc.
  $blazy = $this
    ->formatter()
    ->getBlazy($element);

  // If the caller is Blazy, provides simple index elements.
  if ($settings['namespace'] == 'blazy') {
    $build['items'][$delta] = $blazy;
  }
  else {

    // Otherwise Slick, GridStack, Mason, etc. may need more elements.
    $element[$item_id] = $blazy;

    // Provides extra elements.
    $this
      ->buildElementExtra($element, $entity, $langcode);

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

    // Build the thumbnail item.
    if (!empty($settings['nav'])) {
      $this
        ->buildElementThumbnail($build, $element, $entity, $delta);
    }
  }
}