You are here

public function ImageFormatter::viewInstantArticle in Facebook Instant Articles 3.x

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldFormatter/ImageFormatter.php \Drupal\fb_instant_articles\Plugin\Field\FieldFormatter\ImageFormatter::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/ImageFormatter.php, line 149

Class

ImageFormatter
Plugin implementation of the 'fbia_image' formatter.

Namespace

Drupal\fb_instant_articles\Plugin\Field\FieldFormatter

Code

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($images = $this
    ->getEntitiesToView($items, $langcode))) {

    // Early opt-out if the field is empty.
    return;
  }

  /** @var \Drupal\image\ImageStyleInterface $image_style */
  $image_style = $this->imageStyleStorage
    ->load($this
    ->getSetting('image_style'));

  /** @var \Drupal\file\FileInterface[] $images */
  foreach ($images as $delta => $image) {
    $image_uri = $image
      ->getFileUri();
    $url = $image_style ? $image_style
      ->buildUrl($image_uri) : file_create_url($image_uri);

    // Use the canonical URL override if set.
    if ($canonical_url = $this->config
      ->get('canonical_url_override')) {
      $url = preg_replace('~^https?://.*?(?=/)~', rtrim($canonical_url, '/'), $url);
    }
    $article_image = Image::create()
      ->withURL($url);
    if ($this
      ->getSetting('caption') && isset($image->_referringItem) && ($caption = $image->_referringItem->alt)) {
      $article_image
        ->withCaption(Caption::create()
        ->appendText($caption));
    }
    if ($presentation = $this
      ->getSetting('presentation')) {
      $article_image
        ->withPresentation($presentation);
    }

    // Images can either go in the header as the cover image, or in the body.
    if ($region === Regions::REGION_HEADER) {
      $header = $article
        ->getHeader();
      if (!$header) {
        $header = Header::create();
        $article
          ->withHeader($header);
      }
      $header
        ->withCover($article_image);
    }
    else {
      $article
        ->addChild($article_image);
    }
  }
}