View source
<?php
namespace Drupal\metatag_views\Form;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\metatag\MetatagManagerInterface;
use Drupal\metatag_views\MetatagViewsValuesCleanerTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MetatagViewsEditForm extends FormBase {
use MetatagViewsValuesCleanerTrait;
protected $metatagManager;
protected $viewsManager;
protected $display;
protected $view;
public function __construct(MetatagManagerInterface $metatag_manager, EntityTypeManagerInterface $entity_type_manager) {
$this->metatagManager = $metatag_manager;
$this->viewsManager = $entity_type_manager
->getStorage('view');
}
public static function create(ContainerInterface $container) {
return new static($container
->get('metatag.manager'), $container
->get('entity_type.manager'));
}
public function getFormId() {
return 'metatag_views_edit_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$view_id = $this
->getRequest()
->get('view_id');
$display_id = $this
->getRequest()
->get('display_id');
$metatags = [];
if ($view_id && $display_id) {
$metatags = metatag_get_view_tags($view_id, $display_id);
}
$form['metatags'] = $this->metatagManager
->form($metatags, $form, [
'view',
]);
$form['metatags']['#title'] = $this
->t('Metatags');
$form['metatags']['#type'] = 'fieldset';
$form['view'] = [
'#type' => 'value',
'#title' => $this
->t('View'),
'#weight' => -100,
'#default_value' => $view_id . ':' . $display_id,
'#required' => TRUE,
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Submit'),
];
return $form;
}
public function form(array $values, array $element, array $token_types = [], array $included_groups = NULL, array $included_tags = NULL, $verbose_help = FALSE) {
$element += [
'#type' => 'details',
];
$element += $this->tokenService
->tokenBrowser($token_types, $verbose_help);
$groups_and_tags = $this
->sortedGroupsWithTags();
$first = TRUE;
foreach ($groups_and_tags as $group_id => $group) {
if (isset($group['tags']) && (is_null($included_groups) || in_array($group_id, $included_groups))) {
$element[$group_id]['#type'] = 'details';
$element[$group_id]['#title'] = $group['label'];
$element[$group_id]['#description'] = $group['description'];
$element[$group_id]['#open'] = $first;
$first = FALSE;
foreach ($group['tags'] as $tag_id => $tag) {
if (is_null($included_tags) || in_array($tag_id, $included_tags)) {
$tag = $this->tagPluginManager
->createInstance($tag_id);
$tag_value = isset($values[$tag_id]) ? $values[$tag_id] : NULL;
$tag
->setValue($tag_value);
$element[$group_id][$tag_id] = $tag
->form($element);
}
}
}
}
return $element;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$view_name = $form_state
->getValue('view');
list($view_id, $display_id) = explode(':', $view_name);
$metatags = $form_state
->getValues();
unset($metatags['view']);
$metatags = $this
->clearMetatagViewsDisallowedValues($metatags);
$view = $this->viewsManager
->load($view_id);
$config_name = $view
->getConfigDependencyName();
$config_path = 'display.' . $display_id . '.display_options.display_extenders.metatag_display_extender.metatags';
$configuration = $this
->configFactory()
->getEditable($config_name);
if (empty($this
->removeEmptyTags($metatags))) {
$configuration
->clear($config_path);
}
else {
$configuration
->set($config_path, $metatags);
}
$configuration
->save();
$form_state
->setRedirect('metatag_views.metatags.list');
$this
->messenger()
->addMessage($this
->t('Metatags for @view : @display have been saved.', [
'@view' => $view
->label(),
'@display' => $view
->getDisplay($display_id)['display_title'],
]));
}
}