3 namespace Drupal\metatag_views\Form;
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;
13 * Class MetatagViewsEditForm.
15 * @package Drupal\metatag_views\Form
17 class MetatagViewsEditForm extends FormBase {
19 use MetatagViewsValuesCleanerTrait;
22 * Drupal\metatag\MetatagManager definition.
24 * @var \Drupal\metatag\MetatagManager
26 protected $metatagManager;
31 * @var \Drupal\Core\Entity\EntityStorageInterface
33 protected $viewsManager;
36 * Array of display settings from ViewEntityInterface::getDisplay().
45 * @var \Drupal\views\ViewEntityInterface
52 public function __construct(MetatagManagerInterface $metatag_manager, EntityTypeManagerInterface $entity_manager) {
53 $this->metatagManager = $metatag_manager;
54 $this->viewsManager = $entity_manager->getStorage('view');
60 public static function create(ContainerInterface $container) {
62 $container->get('metatag.manager'),
63 $container->get('entity_type.manager')
70 public function getFormId() {
71 return 'metatag_views_edit_form';
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');
82 // Get meta tags from the view entity.
84 if ($view_id && $display_id) {
85 $metatags = metatag_get_view_tags($view_id, $display_id);
88 $form['metatags'] = $this->metatagManager->form($metatags, $form, ['view']);
89 $form['metatags']['#title'] = $this->t('Metatags');
90 $form['metatags']['#type'] = 'fieldset';
92 // Need to create that AFTER the $form['metatags'] as the whole form is
93 // passed to the $metatagManager->form() which causes duplicated field.
96 '#title' => $this->t('View'),
98 '#default_value' => $view_id . ':' . $display_id,
102 $form['actions']['submit'] = [
104 '#value' => $this->t('Submit'),
113 public function form(array $values, array $element, array $token_types = [], array $included_groups = NULL, array $included_tags = NULL) {
114 // Add the outer fieldset.
116 '#type' => 'details',
119 $element += $this->tokenService->tokenBrowser($token_types);
121 $groups_and_tags = $this->sortedGroupsWithTags();
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;
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);
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);
145 // Create the bit of form for this tag.
146 $element[$group_id][$tag_id] = $tag->form($element);
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);
163 $metatags = $form_state->getValues();
164 unset($metatags['view']);
165 $metatags = $this->clearMetatagViewsDisallowedValues($metatags);
167 /** @var \Drupal\views\ViewEntityInterface $view */
168 $view = $this->viewsManager->load($view_id);
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';
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);
181 $configuration->set($config_path, $metatags);
183 $configuration->save();
185 // Redirect back to the views list.
186 $form_state->setRedirect('metatag_views.metatags.list');
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'],