Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / metatag / src / Plugin / Field / FieldWidget / MetatagFirehose.php
1 <?php
2
3 namespace Drupal\metatag\Plugin\Field\FieldWidget;
4
5 use Drupal\Core\Field\FieldItemListInterface;
6 use Drupal\Core\Field\WidgetBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Field\FieldDefinitionInterface;
9 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
10 use Drupal\metatag\MetatagManager;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12
13 /**
14  * Advanced widget for metatag field.
15  *
16  * @FieldWidget(
17  *   id = "metatag_firehose",
18  *   label = @Translation("Advanced meta tags form"),
19  *   field_types = {
20  *     "metatag"
21  *   }
22  * )
23  */
24 class MetatagFirehose extends WidgetBase implements ContainerFactoryPluginInterface {
25
26   /**
27    * Instance of MetatagManager service.
28    *
29    * @var \Drupal\metatag\MetatagManager
30    */
31   protected $metatagManager;
32
33   /**
34    * {@inheritdoc}
35    */
36   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
37     return new static(
38       $plugin_id,
39       $plugin_definition,
40       $configuration['field_definition'],
41       $configuration['settings'],
42       $configuration['third_party_settings'],
43       $container->get('metatag.manager')
44     );
45   }
46
47   /**
48    * {@inheritdoc}
49    */
50   public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, MetatagManager $manager) {
51     parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings);
52     $this->metatagManager = $manager;
53   }
54
55   /**
56    * {@inheritdoc}
57    */
58   public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
59     $item = $items[$delta];
60     $default_tags = metatag_get_default_tags();
61
62     // Retrieve the values for each metatag from the serialized array.
63     $values = [];
64     if (!empty($item->value)) {
65       $values = unserialize($item->value);
66     }
67
68     // Populate fields which have not been overridden in the entity.
69     if (!empty($default_tags)) {
70       foreach ($default_tags as $tag_id => $tag_value) {
71         if (!isset($values[$tag_id]) && !empty($tag_value)) {
72           $values[$tag_id] = $tag_value;
73         }
74       }
75     }
76
77     // Retrieve configuration settings.
78     $settings = \Drupal::config('metatag.settings');
79     $entity_type_groups = $settings->get('entity_type_groups');
80
81     // Find the current entity type and bundle.
82     $entity_type = $item->getEntity()->getentityTypeId();
83     $entity_bundle = $item->getEntity()->bundle();
84
85     // See if there are requested groups for this entity type and bundle.
86     $groups = !empty($entity_type_groups[$entity_type]) && !empty($entity_type_groups[$entity_type][$entity_bundle]) ? $entity_type_groups[$entity_type][$entity_bundle] : [];
87     // Limit the form to requested groups, if any.
88     if (!empty($groups)) {
89       $element = $this->metatagManager->form($values, $element, [$entity_type], $groups);
90     }
91     // Otherwise, display all groups.
92     else {
93       $element = $this->metatagManager->form($values, $element, [$entity_type]);
94     }
95
96     // Put the form element into the form's "advanced" group.
97     $element['#group'] = 'advanced';
98
99     return $element;
100   }
101
102   /**
103    * {@inheritdoc}
104    */
105   public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
106     // Flatten the values array to remove the groups and then serialize all the
107     // meta tags into one value for storage.
108     $tag_manager = \Drupal::service('plugin.manager.metatag.tag');
109     foreach ($values as &$value) {
110       $flattened_value = [];
111       foreach ($value as $group) {
112         // Exclude the "original delta" value.
113         if (is_array($group)) {
114           foreach ($group as $tag_id => $tag_value) {
115             $tag = $tag_manager->createInstance($tag_id);
116             $tag->setValue($tag_value);
117             if (!empty($tag->value())) {
118               $flattened_value[$tag_id] = $tag->value();
119             }
120           }
121         }
122       }
123       $value = serialize($flattened_value);
124     }
125
126     return $values;
127   }
128
129 }