3 namespace Drupal\ctools\Form;
5 use Drupal\Component\Plugin\PluginManagerInterface;
6 use Drupal\Core\Form\ConfirmFormBase;
7 use Drupal\Core\Form\ConfirmFormHelper;
8 use Drupal\Core\Form\FormStateInterface;
10 use Drupal\ctools\ConstraintConditionInterface;
11 use Drupal\user\SharedTempStoreFactory;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
14 abstract class ConditionDelete extends ConfirmFormBase {
17 * @var \Drupal\user\SharedTempStoreFactory
22 * @var \Drupal\Core\Condition\ConditionManager
29 protected $tempstore_id;
34 protected $machine_name;
44 public static function create(ContainerInterface $container) {
45 return new static($container->get('user.shared_tempstore'), $container->get('plugin.manager.condition'));
48 function __construct(SharedTempStoreFactory $tempstore, PluginManagerInterface $manager) {
49 $this->tempstore = $tempstore;
50 $this->manager = $manager;
56 public function getFormId() {
57 return 'ctools_condition_delete';
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;
68 $cached_values = $this->tempstore->get($this->tempstore_id)->get($this->machine_name);
69 $form ['#title'] = $this->getQuestion($id, $cached_values);
71 $form ['#attributes']['class'][] = 'confirmation';
72 $form ['description'] = array('#markup' => $this->getDescription());
73 $form [$this->getFormName()] = array('#type' => 'hidden', '#value' => 1);
75 // By default, render the form using theme_confirm_form().
76 if (!isset($form ['#theme'])) {
77 $form ['#theme'] = 'confirm_form';
79 $form['actions'] = array('#type' => 'actions');
80 $form['actions'] += $this->actions($form, $form_state);
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));
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);
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'],
112 public function getDescription() {
113 return $this->t('This action cannot be undone.');
119 public function getFormName() {
126 protected function actions(array $form, FormStateInterface $form_state) {
130 '#value' => $this->getConfirmText(),
131 '#validate' => array(
132 array($this, 'validateForm'),
135 array($this, 'submitForm'),
138 'cancel' => ConfirmFormHelper::buildCancelLink($this, $this->getRequest()),
143 * Returns the route to go to if the user cancels the action.
145 * @return \Drupal\Core\Url
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);
157 public function getConfirmText() {
158 return $this->t('Delete');
164 public function getCancelText() {
165 return $this->t('Cancel');
169 * Document the route name and parameters for redirect after submission.
171 * @param $cached_values
175 * return ['route.name', ['machine_name' => $this->machine_name, 'step' => 'step_name]];
177 abstract protected function getParentRouteInfo($cached_values);
180 * Custom logic for retrieving the conditions array from cached_values.
182 * @param $cached_values
186 abstract protected function getConditions($cached_values);
189 * Custom logic for setting the conditions array in cached_values.
191 * @param $cached_values
194 * The conditions to set within the cached values.
197 * Return the $cached_values
199 abstract protected function setConditions($cached_values, $conditions);
202 * Custom logic for retrieving the contexts array from cached_values.
204 * @param $cached_values
206 * @return \Drupal\Core\Plugin\Context\ContextInterface[]
208 abstract protected function getContexts($cached_values);