6f7af554f1d6c2225764b233e3e375a6e455fda9
[yaffs-website] / web / modules / contrib / metatag / metatag_views / src / Form / MetatagViewsRevertForm.php
1 <?php
2
3 namespace Drupal\metatag_views\Form;
4
5 use Drupal\Core\Entity\EntityTypeManagerInterface;
6 use Drupal\Core\Form\ConfirmFormBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Url;
9 use Drupal\views\ViewEntityInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * Defines a confirmation form for deleting mymodule data.
14  */
15 class MetatagViewsRevertForm extends ConfirmFormBase {
16
17   /**
18    * Entity manager for views entities.
19    *
20    * @var EntityTypeManagerInterface
21    */
22   protected $viewsManager;
23
24   /**
25    * The view entity to revert metatags on.
26    *
27    * @var ViewEntityInterface $view
28    */
29   protected $view;
30
31   /**
32    * The view's display id.
33    *
34    * @var string
35    */
36   protected $display_id;
37
38   /**
39    * Constructor.
40    */
41   public function __construct(EntityTypeManagerInterface $entity_manager) {
42     $this->viewsManager = $entity_manager->getStorage('view');
43   }
44
45   /**
46    * {@inheritdoc}
47    */
48   public static function create(ContainerInterface $container) {
49     return new static(
50       $container->get('entity_type.manager')
51     );
52   }
53
54   /**
55    * {@inheritdoc}
56    */
57   public function getFormId() {
58     return 'metatag_views_revert_form';
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   public function getQuestion() {
65     return $this->t('Do you want to revert metatags for @view_name : @display_name?', [
66       '@view_name' => $this->view->label(),
67       '@display_name' => $this->view->getDisplay($this->display_id)['display_title'],
68     ]);
69   }
70
71   /**
72    * {@inheritdoc}
73    */
74   public function getCancelUrl() {
75     return new Url('metatag_views.metatags.list');
76   }
77
78   /**
79    * {@inheritdoc}
80    */
81   public function getDescription() {
82     return $this->t('You are about to revert the custom metatags for the %display_name display on the %view_name view. This action cannot be undone.', [
83       '%view_name' => $this->view->label(),
84       '%display_name' => $this->view->getDisplay($this->display_id)['display_title'],
85     ]);
86   }
87
88   /**
89    * {@inheritdoc}
90    */
91   public function getConfirmText() {
92     return $this->t('Revert');
93   }
94
95   /**
96    * {@inheritdoc}
97    */
98   public function getCancelText() {
99     return $this->t('Cancel');
100   }
101
102   /**
103    * {@inheritdoc}
104    *
105    * @param int $id
106    *   (optional) The ID of the item to be deleted.
107    */
108   public function buildForm(array $form, FormStateInterface $form_state, $view_id = NULL, $display_id = NUL) {
109     $this->view = $this->viewsManager->load($view_id);
110     $this->display_id = $display_id;
111
112     return parent::buildForm($form, $form_state);
113   }
114
115   /**
116    * {@inheritdoc}
117    */
118   public function submitForm(array &$form, FormStateInterface $form_state) {
119     // Removed metatags from the view.
120     $config_name = $this->view->getConfigDependencyName();
121     $config_path = 'display.' . $this->display_id . '.display_options.display_extenders.metatag_display_extender.metatags';
122
123     $configuration = $this->configFactory()->getEditable($config_name)
124       ->clear($config_path)
125       ->save();
126
127     // Redirect back to the views list.
128     $form_state->setRedirect('metatag_views.metatags.list');
129
130     drupal_set_message($this->t('Reverted metatags for @view_name : @display_name', [
131       '@view_name' => $this->view->label(),
132       '@display_name' => $this->view->getDisplay($this->display_id)['display_title'],
133     ]));
134   }
135
136 }