32837dcd68897f70671666db91559c1a0cd6f666
[yaffs-website] / web / modules / contrib / metatag / src / Normalizer / MetatagNormalizer.php
1 <?php
2
3 namespace Drupal\metatag\Normalizer;
4
5 use Drupal\serialization\Normalizer\NormalizerBase;
6
7 /**
8  * Normalizes metatag into the viewed entity.
9  */
10 class MetatagNormalizer extends NormalizerBase {
11
12   /**
13    * {@inheritdoc}
14    */
15   protected $supportedInterfaceOrClass = 'Drupal\metatag\Plugin\Field\MetatagEntityFieldItemList';
16
17   /**
18    * {@inheritdoc}
19    */
20   public function normalize($field_item, $format = NULL, array $context = []) {
21     // @see metatag_get_tags_from_route()
22     $entity = $field_item->getEntity();
23
24     $tags = metatag_get_tags_from_route($entity);
25
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'];
32         }
33         elseif (isset($tag[0]['#attributes']['href'])) {
34           $normalized['value'][$tag[1]] = $tag[0]['#attributes']['href'];
35         }
36       }
37     }
38
39     if (isset($context['langcode'])) {
40       $normalized['lang'] = $context['langcode'];
41     }
42
43     return $normalized;
44   }
45
46   /**
47    * {@inheritdoc}
48    */
49   public function supportsDenormalization($data, $type, $format = NULL) {
50     return FALSE;
51   }
52
53 }