You are here

public function SlickMediaFormatter::viewElements in Slick Media 8.2

Same name and namespace in other branches
  1. 8 src/Plugin/Field/FieldFormatter/SlickMediaFormatter.php \Drupal\slick_media\Plugin\Field\FieldFormatter\SlickMediaFormatter::viewElements()

File

src/Plugin/Field/FieldFormatter/SlickMediaFormatter.php, line 47

Class

SlickMediaFormatter
Plugin implementation of the 'slick media entity' formatter.

Namespace

Drupal\slick_media\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $entities = $this
    ->getEntitiesToView($items, $langcode);

  // Early opt-out if the field is empty.
  if (empty($entities)) {
    return [];
  }

  // Collects specific settings to this formatter.
  $settings = $this
    ->getSettings();

  // Asks for Blazy to deal with iFrames, and mobile-optimized lazy loading.
  $settings['blazy'] = TRUE;
  $settings['plugin_id'] = $this
    ->getPluginId();

  // Sets dimensions once to reduce method ::transformDimensions() calls.
  // @todo: A more flexible way to also support paragraphs at one go.
  $entities = array_values($entities);
  if (!empty($settings['image_style']) && $entities[0]
    ->getEntityTypeId() == 'media') {
    $fields = $entities[0]
      ->getFields();
    if (isset($fields['thumbnail'])) {
      $item = $fields['thumbnail']
        ->get(0);
      $settings['item'] = $item;
      $settings['uri'] = $item->entity
        ->getFileUri();
    }
  }
  $build = [
    'settings' => $settings,
  ];
  $this->formatter
    ->buildSettings($build, $items);

  // Build the elements.
  $this
    ->buildElements($build, $entities, $langcode);
  return $this
    ->manager()
    ->build($build);
}