3 namespace Drupal\metatag\Plugin\Field\FieldWidget;
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;
14 * Advanced widget for metatag field.
17 * id = "metatag_firehose",
18 * label = @Translation("Advanced meta tags form"),
24 class MetatagFirehose extends WidgetBase implements ContainerFactoryPluginInterface {
27 * Instance of MetatagManager service.
29 * @var \Drupal\metatag\MetatagManager
31 protected $metatagManager;
36 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
40 $configuration['field_definition'],
41 $configuration['settings'],
42 $configuration['third_party_settings'],
43 $container->get('metatag.manager')
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;
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();
62 // Retrieve the values for each metatag from the serialized array.
64 if (!empty($item->value)) {
65 $values = unserialize($item->value);
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;
77 // Retrieve configuration settings.
78 $settings = \Drupal::config('metatag.settings');
79 $entity_type_groups = $settings->get('entity_type_groups');
81 // Find the current entity type and bundle.
82 $entity_type = $item->getEntity()->getentityTypeId();
83 $entity_bundle = $item->getEntity()->bundle();
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);
91 // Otherwise, display all groups.
93 $element = $this->metatagManager->form($values, $element, [$entity_type]);
96 // Put the form element into the form's "advanced" group.
97 $element['#group'] = 'advanced';
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();
123 $value = serialize($flattened_value);