d2303bc8617709d2e179c4a7b989193d53073ce5
[yaffs-website] / web / modules / contrib / ctools / src / Form / ConditionDelete.php
1 <?php
2
3 namespace Drupal\ctools\Form;
4
5 use Drupal\Component\Plugin\PluginManagerInterface;
6 use Drupal\Core\Form\ConfirmFormBase;
7 use Drupal\Core\Form\ConfirmFormHelper;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Url;
10 use Drupal\ctools\ConstraintConditionInterface;
11 use Drupal\user\SharedTempStoreFactory;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13
14 abstract class ConditionDelete extends ConfirmFormBase {
15
16   /**
17    * @var \Drupal\user\SharedTempStoreFactory
18    */
19   protected $tempstore;
20
21   /**
22    * @var \Drupal\Core\Condition\ConditionManager
23    */
24   protected $manager;
25
26   /**
27    * @var string
28    */
29   protected $tempstore_id;
30
31   /**
32    * @var string;
33    */
34   protected $machine_name;
35
36   /**
37    * @var int;
38    */
39   protected $id;
40
41   /**
42    * {@inheritdoc}
43    */
44   public static function create(ContainerInterface $container) {
45     return new static($container->get('user.shared_tempstore'), $container->get('plugin.manager.condition'));
46   }
47
48   function __construct(SharedTempStoreFactory $tempstore, PluginManagerInterface $manager) {
49     $this->tempstore = $tempstore;
50     $this->manager = $manager;
51   }
52
53   /**
54    * {@inheritdoc}
55    */
56   public function getFormId() {
57     return 'ctools_condition_delete';
58   }
59
60   /**
61    * {@inheritdoc}
62    */
63   public function buildForm(array $form, FormStateInterface $form_state, $id = NULL, $tempstore_id = NULL, $machine_name = NULL) {
64     $this->tempstore_id = $tempstore_id;
65     $this->machine_name = $machine_name;
66     $this->id = $id;
67
68     $cached_values = $this->tempstore->get($this->tempstore_id)->get($this->machine_name);
69     $form ['#title'] = $this->getQuestion($id, $cached_values);
70
71     $form ['#attributes']['class'][] = 'confirmation';
72     $form ['description'] = array('#markup' => $this->getDescription());
73     $form [$this->getFormName()] = array('#type' => 'hidden', '#value' => 1);
74
75     // By default, render the form using theme_confirm_form().
76     if (!isset($form ['#theme'])) {
77       $form ['#theme'] = 'confirm_form';
78     }
79     $form['actions'] = array('#type' => 'actions');
80     $form['actions'] += $this->actions($form, $form_state);
81     return $form;
82   }
83
84   /**
85    * {@inheritdoc}
86    */
87   public function submitForm(array &$form, FormStateInterface $form_state) {
88     $cached_values = $this->tempstore->get($this->tempstore_id)->get($this->machine_name);
89     $conditions = $this->getConditions($cached_values);
90     /** @var  $instance \Drupal\ctools\ConstraintConditionInterface */
91     $instance = $this->manager->createInstance($conditions[$this->id]['id'], $conditions[$this->id]);
92     if ($instance instanceof ConstraintConditionInterface) {
93       $instance->removeConstraints($this->getContexts($cached_values));
94     }
95     unset($conditions[$this->id]);
96     $cached_values = $this->setConditions($cached_values, $conditions);
97     $this->tempstore->get($this->tempstore_id)->set($this->machine_name, $cached_values);
98     list($route_name, $route_parameters) = $this->getParentRouteInfo($cached_values);
99     $form_state->setRedirect($route_name, $route_parameters);
100   }
101
102   public function getQuestion($id = NULL, $cached_values = NULL) {
103     $condition = $this->getConditions($cached_values)[$id];
104     return $this->t('Are you sure you want to delete the @label condition?', array(
105       '@label' => $condition['id'],
106     ));
107   }
108
109   /**
110    * {@inheritdoc}
111    */
112   public function getDescription() {
113     return $this->t('This action cannot be undone.');
114   }
115
116   /**
117    * {@inheritdoc}
118    */
119   public function getFormName() {
120     return 'confirm';
121   }
122
123   /**
124    * {@inheritdoc}
125    */
126   protected function actions(array $form, FormStateInterface $form_state) {
127     return array(
128       'submit' => array(
129         '#type' => 'submit',
130         '#value' => $this->getConfirmText(),
131         '#validate' => array(
132           array($this, 'validateForm'),
133         ),
134         '#submit' => array(
135           array($this, 'submitForm'),
136         ),
137       ),
138       'cancel' => ConfirmFormHelper::buildCancelLink($this, $this->getRequest()),
139     );
140   }
141
142   /**
143    * Returns the route to go to if the user cancels the action.
144    *
145    * @return \Drupal\Core\Url
146    *   A URL object.
147    */
148   public function getCancelUrl() {
149     $cached_values = $this->tempstore->get($this->tempstore_id)->get($this->machine_name);
150     list($route_name, $route_parameters) = $this->getParentRouteInfo($cached_values);
151     return new Url($route_name, $route_parameters);
152   }
153
154   /**
155    * {@inheritdoc}
156    */
157   public function getConfirmText() {
158     return $this->t('Delete');
159   }
160
161   /**
162    * {@inheritdoc}
163    */
164   public function getCancelText() {
165     return $this->t('Cancel');
166   }
167
168   /**
169    * Document the route name and parameters for redirect after submission.
170    *
171    * @param $cached_values
172    *
173    * @return array
174    *   In the format of
175    *   return ['route.name', ['machine_name' => $this->machine_name, 'step' => 'step_name]];
176    */
177   abstract protected function getParentRouteInfo($cached_values);
178
179   /**
180    * Custom logic for retrieving the conditions array from cached_values.
181    *
182    * @param $cached_values
183    *
184    * @return array
185    */
186   abstract protected function getConditions($cached_values);
187
188   /**
189    * Custom logic for setting the conditions array in cached_values.
190    *
191    * @param $cached_values
192    *
193    * @param $conditions
194    *   The conditions to set within the cached values.
195    *
196    * @return mixed
197    *   Return the $cached_values
198    */
199   abstract protected function setConditions($cached_values, $conditions);
200
201   /**
202    * Custom logic for retrieving the contexts array from cached_values.
203    *
204    * @param $cached_values
205    *
206    * @return \Drupal\Core\Plugin\Context\ContextInterface[]
207    */
208   abstract protected function getContexts($cached_values);
209
210 }