fd2608e13c25c160d7de1ed00ce3ea317aa95957
[yaffs-website] / web / modules / contrib / ctools / src / Form / ConditionConfigure.php
1 <?php
2
3 namespace Drupal\ctools\Form;
4
5
6 use Drupal\Component\Plugin\PluginManagerInterface;
7 use Drupal\Component\Uuid\Uuid;
8 use Drupal\Core\Ajax\AjaxResponse;
9 use Drupal\Core\Ajax\CloseModalDialogCommand;
10 use Drupal\Core\Ajax\RedirectCommand;
11 use Drupal\Core\Form\FormBase;
12 use Drupal\Core\Form\FormStateInterface;
13 use Drupal\Core\Plugin\ContextAwarePluginInterface;
14 use Drupal\ctools\ConstraintConditionInterface;
15 use Drupal\user\SharedTempStoreFactory;
16 use Symfony\Component\DependencyInjection\ContainerInterface;
17
18 /**
19  * Base class for condition configur operations.
20  */
21 abstract class ConditionConfigure extends FormBase {
22
23   /**
24    * @var \Drupal\user\SharedTempStoreFactory
25    */
26   protected $tempstore;
27
28   /**
29    * @var \Drupal\Core\Condition\ConditionManager
30    */
31   protected $manager;
32
33   /**
34    * @var string
35    */
36   protected $tempstore_id;
37
38   /**
39    * @var string;
40    */
41   protected $machine_name;
42
43   /**
44    * {@inheritdoc}
45    */
46   public static function create(ContainerInterface $container) {
47     return new static($container->get('user.shared_tempstore'), $container->get('plugin.manager.condition'));
48   }
49
50   function __construct(SharedTempStoreFactory $tempstore, PluginManagerInterface $manager) {
51     $this->tempstore = $tempstore;
52     $this->manager = $manager;
53   }
54
55   /**
56    * {@inheritdoc}
57    */
58   public function getFormId() {
59     return 'ctools_condition_configure';
60   }
61
62   /**
63    * {@inheritdoc}
64    */
65   public function buildForm(array $form, FormStateInterface $form_state, $condition = NULL, $tempstore_id = NULL, $machine_name = NULL) {
66     $this->tempstore_id = $tempstore_id;
67     $this->machine_name = $machine_name;
68     $cached_values = $this->tempstore->get($this->tempstore_id)->get($this->machine_name);
69     if (is_numeric($condition) || Uuid::isValid($condition)) {
70       $id = $condition;
71       $condition = $this->getConditions($cached_values)[$id];
72       $instance = $this->manager->createInstance($condition['id'], $condition);
73     }
74     else {
75       $instance = $this->manager->createInstance($condition, []);
76     }
77     $form_state->setTemporaryValue('gathered_contexts', $this->getContexts($cached_values));
78     /** @var $instance \Drupal\Core\Condition\ConditionInterface */
79     $form = $instance->buildConfigurationForm($form, $form_state);
80     if (isset($id)) {
81       // Conditionally set this form element so that we can update or add.
82       $form['id'] = [
83         '#type' => 'value',
84         '#value' => $id
85       ];
86     }
87     $form['instance'] = [
88       '#type' => 'value',
89       '#value' => $instance
90     ];
91     $form['submit'] = [
92       '#type' => 'submit',
93       '#value' => $this->t('Save'),
94       '#ajax' => [
95         'callback' => [$this, 'ajaxSave'],
96       ]
97     ];
98     return $form;
99   }
100
101   /**
102    * {@inheritdoc}
103    */
104   public function submitForm(array &$form, FormStateInterface $form_state) {
105     $cached_values = $this->tempstore->get($this->tempstore_id)->get($this->machine_name);
106     /** @var $instance \Drupal\Core\Condition\ConditionInterface */
107     $instance = $form_state->getValue('instance');
108     $instance->submitConfigurationForm($form, $form_state);
109     $conditions = $this->getConditions($cached_values);
110     if ($instance instanceof ContextAwarePluginInterface) {
111       /** @var  $instance \Drupal\Core\Plugin\ContextAwarePluginInterface */
112       $context_mapping = $form_state->hasValue('context_mapping')? $form_state->getValue('context_mapping') : [];
113       $instance->setContextMapping($context_mapping);
114     }
115     if ($instance instanceof ConstraintConditionInterface) {
116       /** @var  $instance \Drupal\ctools\ConstraintConditionInterface */
117       $instance->applyConstraints($this->getContexts($cached_values));
118     }
119     if ($form_state->hasValue('id')) {
120       $conditions[$form_state->getValue('id')] = $instance->getConfiguration();
121     }
122     else {
123       $conditions[] = $instance->getConfiguration();
124     }
125     $cached_values = $this->setConditions($cached_values, $conditions);
126     $this->tempstore->get($this->tempstore_id)->set($this->machine_name, $cached_values);
127     list($route_name, $route_parameters) = $this->getParentRouteInfo($cached_values);
128     $form_state->setRedirect($route_name, $route_parameters);
129   }
130
131   public function ajaxSave(array &$form, FormStateInterface $form_state) {
132     $response = new AjaxResponse();
133     $cached_values = $this->tempstore->get($this->tempstore_id)->get($this->machine_name);
134     list($route_name, $route_parameters) = $this->getParentRouteInfo($cached_values);
135     $response->addCommand(new RedirectCommand($this->url($route_name, $route_parameters)));
136     $response->addCommand(new CloseModalDialogCommand());
137     return $response;
138   }
139
140   /**
141    * Document the route name and parameters for redirect after submission.
142    *
143    * @param $cached_values
144    *
145    * @return array
146    *   In the format of
147    *   return ['route.name', ['machine_name' => $this->machine_name, 'step' => 'step_name']];
148    */
149   abstract protected function getParentRouteInfo($cached_values);
150
151   /**
152    * Custom logic for retrieving the conditions array from cached_values.
153    *
154    * @param $cached_values
155    *
156    * @return array
157    */
158   abstract protected function getConditions($cached_values);
159
160   /**
161    * Custom logic for setting the conditions array in cached_values.
162    *
163    * @param $cached_values
164    *
165    * @param $conditions
166    *   The conditions to set within the cached values.
167    *
168    * @return mixed
169    *   Return the $cached_values
170    */
171   abstract protected function setConditions($cached_values, $conditions);
172
173   /**
174    * Custom logic for retrieving the contexts array from cached_values.
175    *
176    * @param $cached_values
177    *
178    * @return \Drupal\Core\Plugin\Context\ContextInterface[]
179    */
180   abstract protected function getContexts($cached_values);
181
182 }