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;
29 * @var \Drupal\Core\Entity\EntityStorageInterface
31 protected $viewsManager;
34 * Array of display settings from ViewEntityInterface::getDisplay().
43 * @var \Drupal\views\ViewEntityInterface
50 public function __construct(MetatagManagerInterface $metatag_manager, EntityTypeManagerInterface $entity_manager) {
51 $this->metatagManager = $metatag_manager;
52 $this->viewsManager = $entity_manager->getStorage('view');
58 public static function create(ContainerInterface $container) {
60 $container->get('metatag.manager'),
61 $container->get('entity_type.manager')
68 public function getFormId() {
69 return 'metatag_views_edit_form';
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');
80 // Get meta tags from the view entity.
82 if ($view_id && $display_id) {
83 $metatags = metatag_get_view_tags($view_id, $display_id);
86 $form['metatags'] = $this->metatagManager->form($metatags, $form, ['view']);
87 $form['metatags']['#title'] = t('Metatags');
88 $form['metatags']['#type'] = 'fieldset';
90 // Need to create that AFTER the $form['metatags'] as the whole form is
91 // passed to the $metatagManager->form() which causes duplicated field.
94 '#title' => $this->t('View'),
96 '#default_value' => $view_id . ':' . $display_id,
100 $form['actions']['submit'] = [
102 '#value' => t('Submit'),
111 public function form(array $values, array $element, array $token_types = [], array $included_groups = NULL, array $included_tags = NULL) {
112 // Add the outer fieldset.
114 '#type' => 'details',
117 $element += $this->tokenService->tokenBrowser($token_types);
119 $groups_and_tags = $this->sortedGroupsWithTags();
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;
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);
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);
143 // Create the bit of form for this tag.
144 $element[$group_id][$tag_id] = $tag->form($element);
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);
161 $metatags = $form_state->getValues();
162 unset($metatags['view']);
163 $metatags = $this->clearMetatagViewsDisallowedValues($metatags);
165 /** @var \Drupal\views\ViewEntityInterface $view */
166 $view = $this->viewsManager->load($view_id);
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';
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);
179 $configuration->set($config_path, $metatags);
181 $configuration->save();
183 // Redirect back to the views list.
184 $form_state->setRedirect('metatag_views.metatags.list');
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'],