You are here

class MetatagNormalizer in Metatag 8

Normalizes metatag into the viewed entity.

Hierarchy

Expanded class hierarchy of MetatagNormalizer

1 file declares its use of MetatagNormalizer
MetatagServiceProvider.php in src/MetatagServiceProvider.php

File

src/Normalizer/MetatagNormalizer.php, line 10

Namespace

Drupal\metatag\Normalizer
View source
class MetatagNormalizer extends NormalizerBase {

  /**
   * {@inheritdoc}
   */
  protected $supportedInterfaceOrClass = 'Drupal\\metatag\\Plugin\\Field\\MetatagEntityFieldItemList';

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

    // @see metatag_get_tags_from_route()
    $entity = $field_item
      ->getEntity();
    $tags = metatag_get_tags_from_route($entity);
    $normalized['value'] = [];
    if (isset($tags['#attached']['html_head'])) {
      foreach ($tags['#attached']['html_head'] as $tag) {

        // @todo Work out a proper, long-term fix for this.
        if (isset($tag[0]['#attributes']['content'])) {
          $normalized['value'][$tag[1]] = $tag[0]['#attributes']['content'];
        }
        elseif (isset($tag[0]['#attributes']['href'])) {
          $normalized['value'][$tag[1]] = $tag[0]['#attributes']['href'];
        }
      }
    }
    if (isset($context['langcode'])) {
      $normalized['lang'] = $context['langcode'];
    }
    return $normalized;
  }

  /**
   * {@inheritdoc}
   */
  public function supportsDenormalization($data, $type, $format = NULL) {
    return FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY constant Name of key for bubbling cacheability metadata via serialization context.
MetatagNormalizer::$supportedInterfaceOrClass protected property The interface or class that this Normalizer supports. Overrides NormalizerBase::$supportedInterfaceOrClass
MetatagNormalizer::normalize public function Normalizes an object into a set of arrays/scalars. 1
MetatagNormalizer::supportsDenormalization public function Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization() Overrides NormalizerBase::supportsDenormalization
NormalizerBase::$format protected property List of formats which supports (de-)normalization. 3
NormalizerBase::addCacheableDependency protected function Adds cacheability if applicable.
NormalizerBase::checkFormat protected function Checks if the provided format is supported by this normalizer. 2
NormalizerBase::supportsNormalization public function Checks whether the given class is supported for normalization by this normalizer. 1