You are here

public function TransformerFormatter::viewInstantArticle in Facebook Instant Articles 8.2

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

Class

TransformerFormatter
Plugin implementation of the 'fbia_transformer' 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) {
    $markup = [
      '#type' => 'processed_text',
      '#text' => $item->value,
      '#format' => $item->format,
      '#langcode' => $item
        ->getLangcode(),
    ];
    $markup = (string) $this->renderer
      ->renderPlain($markup);

    // Note that by passing $article as the first argument, we are implicitly
    // ignoring the $region param and assuming this content goes into the
    // body are exclusively. The Facebook SDK currently only supports using
    // the transformer for body elements.
    $this->transformer
      ->transformString($article, $markup);
    $this
      ->storeTransformerLogs();
  }
}