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