You are here

public function InstagramEmbedFormatter::viewElements in Media entity Instagram 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldFormatter/InstagramEmbedFormatter.php \Drupal\media_entity_instagram\Plugin\Field\FieldFormatter\InstagramEmbedFormatter::viewElements()
  2. 3.x src/Plugin/Field/FieldFormatter/InstagramEmbedFormatter.php \Drupal\media_entity_instagram\Plugin\Field\FieldFormatter\InstagramEmbedFormatter::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 FormatterInterface::viewElements

File

src/Plugin/Field/FieldFormatter/InstagramEmbedFormatter.php, line 64

Class

InstagramEmbedFormatter
Plugin implementation of the 'instagram_embed' formatter.

Namespace

Drupal\media_entity_instagram\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $element = [];
  $settings = $this
    ->getSettings();
  foreach ($items as $delta => $item) {
    $matches = [];
    foreach (Instagram::$validationRegexp as $pattern => $key) {
      if (preg_match($pattern, $this
        ->getEmbedCode($item), $item_matches)) {
        $matches[] = $item_matches;
      }
    }
    if (!empty($matches)) {
      $matches = reset($matches);
    }
    if (!empty($matches['shortcode'])) {
      if ($instagram = $this->fetcher
        ->fetchInstagramEmbed($matches['shortcode'], $settings['hidecaption'], $settings['width'])) {
        $element[$delta] = [
          '#theme' => 'media_entity_instagram_post',
          '#post' => (string) $instagram['html'],
          '#shortcode' => $matches['shortcode'],
        ];
      }
    }
  }
  if (!empty($element)) {
    $element['#attached'] = [
      'library' => [
        'media_entity_instagram/integration',
      ],
    ];
  }
  return $element;
}