MetatagViewsRevertForm.php in Metatag 8
File
metatag_views/src/Form/MetatagViewsRevertForm.php
View source
<?php
namespace Drupal\metatag_views\Form;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MetatagViewsRevertForm extends ConfirmFormBase {
protected $viewsManager;
protected $view;
protected $displayId;
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->viewsManager = $entity_type_manager
->getStorage('view');
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'));
}
public function getFormId() {
return 'metatag_views_revert_form';
}
public function getQuestion() {
return $this
->t('Do you want to revert meta tags for @view_name : @display_name?', [
'@view_name' => $this->view
->label(),
'@display_name' => $this->view
->getDisplay($this->displayId)['display_title'],
]);
}
public function getCancelUrl() {
return new Url('metatag_views.metatags.list');
}
public function getDescription() {
return $this
->t('You are about to revert the custom meta tags for the %display_name display on the %view_name view. This action cannot be undone.', [
'%view_name' => $this->view
->label(),
'%display_name' => $this->view
->getDisplay($this->displayId)['display_title'],
]);
}
public function getConfirmText() {
return $this
->t('Revert');
}
public function getCancelText() {
return $this
->t('Cancel');
}
public function buildForm(array $form, FormStateInterface $form_state, $view_id = NULL, $display_id = NUL) {
$this->view = $this->viewsManager
->load($view_id);
$this->displayId = $display_id;
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$config_name = $this->view
->getConfigDependencyName();
$config_path = 'display.' . $this->displayId . '.display_options.display_extenders.metatag_display_extender.metatags';
$this
->configFactory()
->getEditable($config_name)
->clear($config_path)
->save();
$form_state
->setRedirect('metatag_views.metatags.list');
$this
->messenger()
->addMessage($this
->t('Reverted meta tags for @view_name : @display_name', [
'@view_name' => $this->view
->label(),
'@display_name' => $this->view
->getDisplay($this->displayId)['display_title'],
]));
}
}