You are here

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

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

Class

CopyrightFormatter
Plugin implementation of the 'fbia_copyright' formatter.

Namespace

Drupal\fb_instant_articles\Plugin\Field\FieldFormatter

Code

public function viewInstantArticle(FieldItemListInterface $items, InstantArticle $article, $region, NormalizerInterface $normalizer, $langcode = NULL) {
  foreach ($items as $delta => $item) {

    // Copyright can only go in the footer, put it there and ignore the given
    // $region.
    $footer = $article
      ->getFooter();
    if (!$footer) {
      $footer = Footer::create();
      $article
        ->withFooter($footer);
    }

    // The instant article only takes a single value for copyright. If a value
    // is already set, append to it. Note that the value might also be an
    // object of type Small, in which case we can't append to it, so we just
    // replace it with a string.
    $copyright = $footer
      ->getCopyright();
    if (is_string($copyright)) {
      $copyright .= ' ' . $item->value;
    }
    else {
      $copyright = $item->value;
    }
    $footer
      ->withCopyright($copyright);
  }
}