You are here

public function BlazyEntityReferenceBase::buildElement in Blazy 8

Returns item contents.

Overrides BlazyEntityBase::buildElement

File

src/Dejavu/BlazyEntityReferenceBase.php, line 24

Class

BlazyEntityReferenceBase
Base class for 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'] = empty($settings['item_id']) ? 'box' : $settings['item_id'];
  $view_mode = $settings['view_mode'] = empty($settings['view_mode']) ? 'full' : $settings['view_mode'];
  if (!empty($settings['vanilla'])) {
    return parent::buildElement($build, $entity, $langcode);
  }
  $delta = isset($settings['delta']) ? $settings['delta'] : 0;
  $element = [
    'settings' => $settings,
  ];

  // Built early before stage to allow custom highres video thumbnail later.
  // Implementor must import: Drupal\blazy\Dejavu\BlazyVideoTrait.
  if (method_exists($this, 'getMediaItem')) {
    $this
      ->getMediaItem($element, $entity);
  }

  // Build the main stage.
  $this
    ->buildStage($element, $entity, $langcode);

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

  // Optional image with responsive image, lazyLoad, and lightbox supports.
  $element[$item_id] = empty($element['item']) ? [] : $this
    ->formatter()
    ->getImage($element);

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

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

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

  // 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
      ->getFieldRenderable($entity, $settings['thumbnail_caption'], $view_mode);
    $build['thumb']['items'][$delta] = $element;
  }
}