You are here

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

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

Class

AuthorReferenceFormatter
Plugin implementation of the 'fbia_author_reference' 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\user\UserInterface $entity */
  foreach ($this
    ->getEntitiesToView($items, $langcode) as $delta => $entity) {
    $author = Author::create()
      ->withName($entity
      ->getDisplayName());
    if ($this
      ->getSetting('link')) {
      $author
        ->withURL($entity
        ->toUrl('canonical', [
        'absolute' => TRUE,
      ])
        ->toString());
    }

    // Author's are added to the header of an instant article regardless of
    // the given $region.
    $header = $article
      ->getHeader();
    if (!$header) {
      $header = Header::create();
    }
    $header
      ->addAuthor($author);
  }
}