You are here

class FieldItemListNormalizer in Facebook Instant Articles 8.2

Same name and namespace in other branches
  1. 3.x src/Normalizer/FieldItemListNormalizer.php \Drupal\fb_instant_articles\Normalizer\FieldItemListNormalizer

Normalize FieldItemList object into an Instant Article object.

Hierarchy

Expanded class hierarchy of FieldItemListNormalizer

1 string reference to 'FieldItemListNormalizer'
fb_instant_articles.services.yml in ./fb_instant_articles.services.yml
fb_instant_articles.services.yml
1 service uses FieldItemListNormalizer
serializer.fb_instant_articles.fbia.field_item_list in ./fb_instant_articles.services.yml
Drupal\fb_instant_articles\Normalizer\FieldItemListNormalizer

File

src/Normalizer/FieldItemListNormalizer.php, line 20

Namespace

Drupal\fb_instant_articles\Normalizer
View source
class FieldItemListNormalizer extends NormalizerBase {
  use TransformerLoggingTrait;

  /**
   * {@inheritdoc}
   */
  protected $supportedInterfaceOrClass = 'Drupal\\Core\\Field\\FieldItemListInterface';

  /**
   * {@inheritdoc}
   */
  protected $format = 'fbia';

  /**
   * Renderer service.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * FieldItemListNormalizer constructor.
   *
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The renderer service.
   * @param \Drupal\fb_instant_articles\Transformer $transformer
   *   FBIA SDK transformer object.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   Config factory service.
   * @param \Psr\Log\LoggerInterface $logger
   *   Logger for transformer messages.
   */
  public function __construct(RendererInterface $renderer, Transformer $transformer, ConfigFactoryInterface $config_factory, LoggerInterface $logger) {
    $this->renderer = $renderer;
    $this->transformer = $transformer;
    $this->configFactory = $config_factory;
    $this->logger = $logger;
    $this
      ->setTransformerLogLevel();
  }

  /**
   * {@inheritdoc}
   */
  public function normalize($object, $format = NULL, array $context = []) {

    /** @var \Drupal\Core\Field\FieldItemListInterface $object */
    if (!isset($context['instant_article'])) {
      return;
    }

    /** @var \Facebook\InstantArticles\Elements\InstantArticle $article */
    $article = $context['instant_article'];

    // If we're given an entity_view_display object as context, use that as a
    // mapping to guide the normalization.
    if (isset($context['entity_view_display'])) {

      /** @var \Drupal\Core\Entity\Entity\EntityViewDisplay $display */
      $display = $context['entity_view_display'];
      $formatter = $display
        ->getRenderer($object
        ->getName());
      $component = $display
        ->getComponent($object
        ->getName());
      if ($formatter instanceof InstantArticleFormatterInterface) {
        $formatter
          ->viewInstantArticle($object, $article, $component['region'], $this->serializer);
      }
      elseif ($formatter instanceof FormatterInterface) {
        $formatter
          ->prepareView([
          $object
            ->getEntity()
            ->id() => $object,
        ]);
        $render_array = $formatter
          ->view($object);
        if ($markup = (string) $this->renderer
          ->renderPlain($render_array)) {

          // Determine correct context for transformation.
          $transformer_context = $article;
          if ($component['region'] === Regions::REGION_HEADER) {
            $header = $article
              ->getHeader();
            if (!$header) {
              $header = Header::create();
              $article
                ->withHeader($header);
            }
            $transformer_context = $header;
          }
          elseif ($component['region'] === Regions::REGION_FOOTER) {
            $footer = $article
              ->getFooter();
            if (!$footer) {
              $footer = Footer::create();
              $article
                ->withFooter($footer);
            }
            $transformer_context = $footer;
          }

          // Region-aware transformation of rendered markup.
          // Pass the markup through the Transformer.
          $this->transformer
            ->transformString($transformer_context, $markup);
          $this
            ->storeTransformerLogs();
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY constant Name of key for bubbling cacheability metadata via serialization context.
FieldItemListNormalizer::$format protected property List of formats which supports (de-)normalization. Overrides NormalizerBase::$format
FieldItemListNormalizer::$renderer protected property Renderer service.
FieldItemListNormalizer::$supportedInterfaceOrClass protected property The interface or class that this Normalizer supports. Overrides NormalizerBase::$supportedInterfaceOrClass
FieldItemListNormalizer::normalize public function
FieldItemListNormalizer::__construct public function FieldItemListNormalizer constructor.
NormalizerBase::addCacheableDependency protected function Adds cacheability if applicable.
NormalizerBase::checkFormat protected function Checks if the provided format is supported by this normalizer. 2
NormalizerBase::supportsDenormalization public function Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization() 1
NormalizerBase::supportsNormalization public function Checks whether the given class is supported for normalization by this normalizer. 1
TransformerLoggingTrait::$configFactory protected property Config factory service.
TransformerLoggingTrait::$logger protected property Logger for transformer messages.
TransformerLoggingTrait::$transformer protected property FBIA SDK transformer object.
TransformerLoggingTrait::setTransformerLogLevel protected function Set the transformer log level according to the FBIA setting.
TransformerLoggingTrait::storeTransformerLogs protected function Store the transformer logs if any.