You are here

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

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 FormatterInterface::viewElements

File

src/Plugin/Field/FieldFormatter/AmpSocialPostFormatter.php, line 169

Class

AmpSocialPostFormatter
Plugin implementation of the 'amp_social_post' formatter.

Namespace

Drupal\amp\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];
  $layout = $this
    ->getSetting('layout');
  $width = $this
    ->validWidth($this
    ->getSetting('width'), $this
    ->getSetting('layout'));
  $height = $this
    ->validHeight($this
    ->getSetting('height'), $this
    ->getSetting('layout'));
  $data_embed_as = $this
    ->getSetting('data-embed-as');
  $data_align_center = $this
    ->getSetting('data-align-center');
  $placeholder = $this
    ->getSetting('placeholder');
  foreach ($items as $delta => $item) {
    $elements[$delta]['#type'] = 'amp_social_post';
    $elements[$delta]['#url'] = !empty($item->value) ? $item->value : $item->uri;
    $elements[$delta]['#placeholder'] = $placeholder;
    $elements[$delta]['#attributes']['layout'] = $layout;
    $elements[$delta]['#attributes']['data-embed-as'] = $data_embed_as;
    $elements[$delta]['#attributes']['data-align-center'] = $data_align_center;
    $elements[$delta]['#attributes']['height'] = $height;
    $elements[$delta]['#attributes']['width'] = $width;
  }
  return $elements;
}