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