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