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 Drupal\views\ViewEntityInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
14 * Class MetatagViewsEditForm.
16 * @package Drupal\metatag_views\Form
18 class MetatagViewsEditForm extends FormBase {
20 use MetatagViewsValuesCleanerTrait;
23 * Drupal\metatag\MetatagManager definition.
25 * @var \Drupal\metatag\MetatagManager
27 protected $metatagManager;
30 * @var \Drupal\Core\Entity\EntityStorageInterface
32 protected $viewsManager;
35 * Array of display settings as returned from getDisplay of ViewEntityInterface
44 * @var ViewEntityInterface
48 public function __construct(MetatagManagerInterface $metatag_manager, EntityTypeManagerInterface $entity_manager) {
49 $this->metatagManager = $metatag_manager;
50 $this->viewsManager = $entity_manager->getStorage('view');
53 public static function create(ContainerInterface $container) {
55 $container->get('metatag.manager'),
56 $container->get('entity_type.manager')
63 public function getFormId() {
64 return 'metatag_views_edit_form';
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');
75 // Get metatags from the view entity.
77 if ($view_id && $display_id) {
78 $metatags = metatag_get_view_tags($view_id, $display_id);
81 $form['metatags'] = $this->metatagManager->form($metatags, $form, ['view']);
82 $form['metatags']['#title'] = t('Metatags');
83 $form['metatags']['#type'] = 'fieldset';
85 // Need to create that AFTER the $form['metatags'] as the whole form
86 // is passed to the $metatagManager->form() which causes duplicated field.
89 '#title' => $this->t('View'),
91 '#default_value' => $view_id . ':' . $display_id,
95 $form['submit'] = array(
97 '#value' => t('Submit'),
106 public function form(array $values, array $element, array $token_types = [], array $included_groups = NULL, array $included_tags = NULL) {
108 // Add the outer fieldset.
110 '#type' => 'details',
113 $element += $this->tokenService->tokenBrowser($token_types);
115 $groups_and_tags = $this->sortedGroupsWithTags();
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;
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);
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);
139 // Create the bit of form for this tag.
140 $element[$group_id][$tag_id] = $tag->form($element);
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);
157 $metatags = $form_state->getValues();
158 unset($metatags['view']);
159 $metatags = $this->clearMetatagViewsDisallowedValues($metatags);
161 /** @var ViewEntityInterface $view */
162 $view = $this->viewsManager->load($view_id);
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';
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);
175 $configuration->set($config_path, $metatags);
177 $configuration->save();
179 // Redirect back to the views list.
180 $form_state->setRedirect('metatag_views.metatags.list');
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'],