public function InstagramEmbedFormatter::viewElements in Media entity Instagram 3.x
Same name and namespace in other branches
- 8.2 src/Plugin/Field/FieldFormatter/InstagramEmbedFormatter.php \Drupal\media_entity_instagram\Plugin\Field\FieldFormatter\InstagramEmbedFormatter::viewElements()
- 8 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 OEmbedFormatter::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\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$element = [];
$max_width = $this
->getSetting('max_width');
foreach ($items as $delta => $item) {
$main_property = $item
->getFieldDefinition()
->getFieldStorageDefinition()
->getMainPropertyName();
$value = $item->{$main_property};
if (empty($value)) {
continue;
}
try {
$resource_url = $this->urlResolver
->getResourceUrl($value, $max_width, NULL, $this
->getSettings());
$resource = $this->resourceFetcher
->fetchResource($resource_url);
} catch (ResourceException $exception) {
$this->logger
->error("Could not retrieve the remote URL (@url).", [
'@url' => $value,
]);
continue;
}
switch ($resource
->getType()) {
case Resource::TYPE_LINK:
case Resource::TYPE_PHOTO:
return parent::viewElements($items, $langcode);
default:
$element[$delta] = [
'#theme' => 'media_oembed_iframe',
'#resource' => $resource,
'#media' => IFrameMarkup::create($resource
->getHtml()),
'#attached' => [
'library' => [
'media_entity_instagram/integration',
],
],
];
CacheableMetadata::createFromObject($resource)
->addCacheTags($this->config
->getCacheTags())
->applyTo($element[$delta]);
}
}
return $element;
}