You are here

public function AmpImageFormatter::viewElements in Accelerated Mobile Pages (AMP) 8.3

Same name and namespace in other branches
  1. 8 src/Plugin/Field/FieldFormatter/AmpImageFormatter.php \Drupal\amp\Plugin\Field\FieldFormatter\AmpImageFormatter::viewElements()
  2. 8.2 src/Plugin/Field/FieldFormatter/AmpImageFormatter.php \Drupal\amp\Plugin\Field\FieldFormatter\AmpImageFormatter::viewElements()

Builds a renderable array for a field value.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.

string $langcode: The language that should be used to render the field.

Return value

array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.

Overrides ImageFormatter::viewElements

File

src/Plugin/Field/FieldFormatter/AmpImageFormatter.php, line 89

Class

AmpImageFormatter
Plugin implementation of the 'amp_image' formatter.

Namespace

Drupal\amp\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = parent::viewElements($items, $langcode);
  $layout = $this
    ->getSetting('layout');
  $width = $this
    ->validWidth($this
    ->getSetting('width'), $this
    ->getSetting('layout'));
  $height = $this
    ->validHeight($this
    ->getSetting('height'), $this
    ->getSetting('layout'));
  foreach ($elements as $delta => $element) {
    $elements[$delta]['#item_attributes']['layout'] = $layout;
    $elements[$delta]['#item_attributes']['width'] = $width;
    $elements[$delta]['#item_attributes']['height'] = $height;
    $elements[$delta]['#item_attributes'] = array_filter($elements[$delta]['#item_attributes']);
    $elements[$delta]['#item_attributes']['loading'] = FALSE;
  }
  $elements['#attached']['library'] = $this
    ->getLibraries();
  return $elements;
}