public function VideoFormatter::viewInstantArticle in Facebook Instant Articles 8.2
Same name and namespace in other branches
- 3.x src/Plugin/Field/FieldFormatter/VideoFormatter.php \Drupal\fb_instant_articles\Plugin\Field\FieldFormatter\VideoFormatter::viewInstantArticle()
Modifies the given instant article with the contents of this field.
Parameters
\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.
\Facebook\InstantArticles\Elements\InstantArticle $article: Instant article object to modify, rendering the contents of this field into it.
string $region: The Instant Article region name that the contents of this field should be rendered into.
\Symfony\Component\Serializer\Normalizer\NormalizerInterface $normalizer: Normalizer in case the formatter needs to recursively normalize, eg. in the case of a entity reference field.
string $langcode: (optional) The language that should be used to render the field. Defaults to the current content language.
Overrides InstantArticleFormatterInterface::viewInstantArticle
File
- src/
Plugin/ Field/ FieldFormatter/ VideoFormatter.php, line 143
Class
- VideoFormatter
- Plugin implementation of the 'fbia_video' formatter.
Namespace
Drupal\fb_instant_articles\Plugin\Field\FieldFormatterCode
public function viewInstantArticle(FieldItemListInterface $items, InstantArticle $article, $region, NormalizerInterface $normalizer, $langcode = NULL) {
// Need to call parent::prepareView() to populate the entities since it's
// not otherwise getting called.
$this
->prepareView([
$items
->getEntity()
->id() => $items,
]);
/** @var \Drupal\Core\Field\EntityReferenceFieldItemListInterface $items */
if (empty($videos = $this
->getEntitiesToView($items, $langcode))) {
// Early opt-out if the field is empty.
return;
}
// Prepare the header region if we're adding to the header.
if ($region === Regions::REGION_HEADER) {
$header = $article
->getHeader();
if (!$header) {
$header = Header::create();
$article
->withHeader($header);
}
}
/** @var \Drupal\file\FileInterface[] $videos */
foreach ($videos as $delta => $video) {
// Build the Video Element using configured settings.
$video_uri = file_create_url($video
->getFileUri());
// Use the canonical URL override if set.
if ($canonical_url = $this->config
->get('canonical_url_override')) {
$video_uri = preg_replace('~^https?://.*?(?=/)~', rtrim($canonical_url, '/'), $video_uri);
}
$video = Video::create();
$video
->withURL($video_uri);
if ($presentation = $this
->getSetting('presentation')) {
$video
->withPresentation($presentation);
}
if ($this
->getSetting('controls')) {
$video
->enableControls();
}
else {
$video
->disableControls();
}
if ($this
->getSetting('autoplay')) {
$video
->enableAutoplay();
}
else {
$video
->disableAutoplay();
}
// If this field is marked as the Newsfeed cover, use only the first video
// in a multivalue field as the Newsfeed cover.
if ($this
->getSetting('feed_cover') && $delta === 0) {
$video
->enableFeedCover();
}
else {
$video
->disableFeedCover();
}
// Finally add the video to the article, either as the cover, or in the
// content of the article per the $region param.
if ($region === Regions::REGION_HEADER) {
$header
->withCover($video);
}
else {
$article
->addChild($video);
}
}
}