You are here

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

Same name and namespace in other branches
  1. 8.3 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 110
Contains \Drupal\amp\Plugin\Field\FieldFormatter\AmpImageFormatter.

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);
  foreach ($elements as $delta => $element) {
    $elements[$delta]['#item_attributes']['layout'] = $this
      ->getSetting('amp_layout');
    if ($this
      ->getSetting('amp_layout') == 'fixed-height') {
      $elements[$delta]['#item_attributes']['height'] = $this
        ->getSetting('amp_fixed_height');
      $elements[$delta]['#item_attributes']['width'] = 'auto';
    }
  }
  return $elements;
}