3 namespace Drupal\metatag\Normalizer;
5 use Drupal\serialization\Normalizer\NormalizerBase;
8 * Normalizes metatag into the viewed entity.
10 class MetatagNormalizer extends NormalizerBase {
15 protected $supportedInterfaceOrClass = 'Drupal\metatag\Plugin\Field\MetatagEntityFieldItemList';
20 public function normalize($field_item, $format = NULL, array $context = []) {
21 // @see metatag_get_tags_from_route()
22 $entity = $field_item->getEntity();
24 $tags = metatag_get_tags_from_route($entity);
26 $normalized['value'] = [];
27 if (isset($tags['#attached']['html_head'])) {
28 foreach ($tags['#attached']['html_head'] as $tag) {
29 // @todo Work out a proper, long-term fix for this.
30 if (isset($tag[0]['#attributes']['content'])) {
31 $normalized['value'][$tag[1]] = $tag[0]['#attributes']['content'];
33 elseif (isset($tag[0]['#attributes']['href'])) {
34 $normalized['value'][$tag[1]] = $tag[0]['#attributes']['href'];
39 if (isset($context['langcode'])) {
40 $normalized['lang'] = $context['langcode'];
49 public function supportsDenormalization($data, $type, $format = NULL) {