Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / metatag / metatag_views / src / Form / MetatagViewsEditForm.php
1 <?php
2
3 namespace Drupal\metatag_views\Form;
4
5 use Drupal\Core\Entity\EntityTypeManagerInterface;
6 use Drupal\Core\Form\FormBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\metatag\MetatagManagerInterface;
9 use Drupal\metatag_views\MetatagViewsValuesCleanerTrait;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * Class MetatagViewsEditForm.
14  *
15  * @package Drupal\metatag_views\Form
16  */
17 class MetatagViewsEditForm extends FormBase {
18
19   use MetatagViewsValuesCleanerTrait;
20
21   /**
22    * Drupal\metatag\MetatagManager definition.
23    *
24    * @var \Drupal\metatag\MetatagManager
25    */
26   protected $metatagManager;
27
28   /**
29    * The Views manager.
30    *
31    * @var \Drupal\Core\Entity\EntityStorageInterface
32    */
33   protected $viewsManager;
34
35   /**
36    * Array of display settings from ViewEntityInterface::getDisplay().
37    *
38    * @var array
39    */
40   protected $display;
41
42   /**
43    * View entity object.
44    *
45    * @var \Drupal\views\ViewEntityInterface
46    */
47   protected $view;
48
49   /**
50    * {@inheritdoc}
51    */
52   public function __construct(MetatagManagerInterface $metatag_manager, EntityTypeManagerInterface $entity_manager) {
53     $this->metatagManager = $metatag_manager;
54     $this->viewsManager = $entity_manager->getStorage('view');
55   }
56
57   /**
58    * {@inheritdoc}
59    */
60   public static function create(ContainerInterface $container) {
61     return new static(
62       $container->get('metatag.manager'),
63       $container->get('entity_type.manager')
64     );
65   }
66
67   /**
68    * {@inheritdoc}
69    */
70   public function getFormId() {
71     return 'metatag_views_edit_form';
72   }
73
74   /**
75    * {@inheritdoc}
76    */
77   public function buildForm(array $form, FormStateInterface $form_state) {
78     // Get the parameters from request.
79     $view_id = \Drupal::request()->get('view_id');
80     $display_id = \Drupal::request()->get('display_id');
81
82     // Get meta tags from the view entity.
83     $metatags = [];
84     if ($view_id && $display_id) {
85       $metatags = metatag_get_view_tags($view_id, $display_id);
86     }
87
88     $form['metatags'] = $this->metatagManager->form($metatags, $form, ['view']);
89     $form['metatags']['#title'] = $this->t('Metatags');
90     $form['metatags']['#type'] = 'fieldset';
91
92     // Need to create that AFTER the $form['metatags'] as the whole form is
93     // passed to the $metatagManager->form() which causes duplicated field.
94     $form['view'] = [
95       '#type' => 'value',
96       '#title' => $this->t('View'),
97       '#weight' => -100,
98       '#default_value' => $view_id . ':' . $display_id,
99       '#required' => TRUE,
100     ];
101
102     $form['actions']['submit'] = [
103       '#type' => 'submit',
104       '#value' => $this->t('Submit'),
105     ];
106
107     return $form;
108   }
109
110   /**
111    * {@inheritdoc}
112    */
113   public function form(array $values, array $element, array $token_types = [], array $included_groups = NULL, array $included_tags = NULL) {
114     // Add the outer fieldset.
115     $element += [
116       '#type' => 'details',
117     ];
118
119     $element += $this->tokenService->tokenBrowser($token_types);
120
121     $groups_and_tags = $this->sortedGroupsWithTags();
122
123     $first = TRUE;
124     foreach ($groups_and_tags as $group_id => $group) {
125       // Only act on groups that have tags and are in the list of included
126       // groups (unless that list is null).
127       if (isset($group['tags']) && (is_null($included_groups) || in_array($group_id, $included_groups))) {
128         // Create the fieldset.
129         $element[$group_id]['#type'] = 'details';
130         $element[$group_id]['#title'] = $group['label'];
131         $element[$group_id]['#description'] = $group['description'];
132         $element[$group_id]['#open'] = $first;
133         $first = FALSE;
134
135         foreach ($group['tags'] as $tag_id => $tag) {
136           // Only act on tags in the included tags list, unless that is null.
137           if (is_null($included_tags) || in_array($tag_id, $included_tags)) {
138             // Make an instance of the tag.
139             $tag = $this->tagPluginManager->createInstance($tag_id);
140
141             // Set the value to the stored value, if any.
142             $tag_value = isset($values[$tag_id]) ? $values[$tag_id] : NULL;
143             $tag->setValue($tag_value);
144
145             // Create the bit of form for this tag.
146             $element[$group_id][$tag_id] = $tag->form($element);
147           }
148         }
149       }
150     }
151
152     return $element;
153   }
154
155   /**
156    * {@inheritdoc}
157    */
158   public function submitForm(array &$form, FormStateInterface $form_state) {
159     // Get the submitted form values.
160     $view_name = $form_state->getValue('view');
161     list($view_id, $display_id) = explode(':', $view_name);
162
163     $metatags = $form_state->getValues();
164     unset($metatags['view']);
165     $metatags = $this->clearMetatagViewsDisallowedValues($metatags);
166
167     /** @var \Drupal\views\ViewEntityInterface $view */
168     $view = $this->viewsManager->load($view_id);
169
170     // Store the meta tags on the view.
171     $config_name = $view->getConfigDependencyName();
172     $config_path = 'display.' . $display_id . '.display_options.display_extenders.metatag_display_extender.metatags';
173
174     // Set configuration values based on form submission. This always edits the
175     // original language.
176     $configuration = $this->configFactory()->getEditable($config_name);
177     if (empty($this->removeEmptyTags($metatags))) {
178       $configuration->clear($config_path);
179     }
180     else {
181       $configuration->set($config_path, $metatags);
182     }
183     $configuration->save();
184
185     // Redirect back to the views list.
186     $form_state->setRedirect('metatag_views.metatags.list');
187
188     drupal_set_message($this->t('Metatags for @view : @display have been saved.', [
189       '@view' => $view->label(),
190       '@display' => $view->getDisplay($display_id)['display_title'],
191     ]));
192   }
193
194 }