You are here

public function InstantArticleContentEntityNormalizer::normalize in Facebook Instant Articles 3.x

Same name and namespace in other branches
  1. 8.2 src/Normalizer/InstantArticleContentEntityNormalizer.php \Drupal\fb_instant_articles\Normalizer\InstantArticleContentEntityNormalizer::normalize()

File

src/Normalizer/InstantArticleContentEntityNormalizer.php, line 111

Class

InstantArticleContentEntityNormalizer
Facebook Instant Articles content entity normalizer.

Namespace

Drupal\fb_instant_articles\Normalizer

Code

public function normalize($data, $format = NULL, array $context = []) {

  /** @var \Drupal\Core\Entity\ContentEntityInterface $data */
  if (isset($context['instant_article'])) {
    $article = $context['instant_article'];
  }
  else {
    $article = InstantArticle::create()
      ->addMetaProperty('op:generator:application', 'drupal/fb_instant_articles')
      ->addMetaProperty('op:generator:application:version', $this
      ->getApplicationVersion());

    // RTL support.
    if ($this->currentLanguage
      ->getDirection() === LanguageInterface::DIRECTION_RTL) {
      $article
        ->enableRTL();
    }

    // Configured style.
    if ($style = $this->config
      ->get('style')) {
      $article
        ->withStyle($style);
    }
    $this
      ->normalizeCanonicalUrl($article, $data);
    $this
      ->normalizeDefaultHeader($article, $data);
    $this
      ->analyticsFromSettings($article);
    $this
      ->adsFromSettings($article);
    $context += [
      'instant_article' => $article,
    ];
  }

  // If we're given an entity_view_display object as context, use that as a
  // mapping to guide the normalization.
  if ($display = $this
    ->entityViewDisplay($data, $context)) {

    // Declare a dependency on the view mode configuration if we are rendering
    // in the context of a views REST export.
    if (isset($context['views_style_plugin'])) {
      $context['views_style_plugin']->displayHandler->display['cache_metadata']['tags'] = Cache::mergeTags($context['views_style_plugin']->displayHandler->display['cache_metadata']['tags'], $display
        ->getCacheTags());
    }
    $context['entity_view_display'] = $display;
    $components = $this
      ->getApplicableComponents($display);
    uasort($components, [
      $this,
      'sortComponents',
    ]);
    foreach ($components as $name => $options) {
      $this->serializer
        ->normalize($data
        ->get($name), $format, $context);
    }
  }
  return $article;
}