3 namespace Drupal\ctools\Form;
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;
19 * Base class for condition configur operations.
21 abstract class ConditionConfigure extends FormBase {
24 * @var \Drupal\user\SharedTempStoreFactory
29 * @var \Drupal\Core\Condition\ConditionManager
36 protected $tempstore_id;
41 protected $machine_name;
46 public static function create(ContainerInterface $container) {
47 return new static($container->get('user.shared_tempstore'), $container->get('plugin.manager.condition'));
50 function __construct(SharedTempStoreFactory $tempstore, PluginManagerInterface $manager) {
51 $this->tempstore = $tempstore;
52 $this->manager = $manager;
58 public function getFormId() {
59 return 'ctools_condition_configure';
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)) {
71 $condition = $this->getConditions($cached_values)[$id];
72 $instance = $this->manager->createInstance($condition['id'], $condition);
75 $instance = $this->manager->createInstance($condition, []);
77 $form_state->setTemporaryValue('gathered_contexts', $this->getContexts($cached_values));
78 /** @var $instance \Drupal\Core\Condition\ConditionInterface */
79 $form = $instance->buildConfigurationForm($form, $form_state);
81 // Conditionally set this form element so that we can update or add.
93 '#value' => $this->t('Save'),
95 'callback' => [$this, 'ajaxSave'],
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);
115 if ($instance instanceof ConstraintConditionInterface) {
116 /** @var $instance \Drupal\ctools\ConstraintConditionInterface */
117 $instance->applyConstraints($this->getContexts($cached_values));
119 if ($form_state->hasValue('id')) {
120 $conditions[$form_state->getValue('id')] = $instance->getConfiguration();
123 $conditions[] = $instance->getConfiguration();
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);
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());
141 * Document the route name and parameters for redirect after submission.
143 * @param $cached_values
147 * return ['route.name', ['machine_name' => $this->machine_name, 'step' => 'step_name']];
149 abstract protected function getParentRouteInfo($cached_values);
152 * Custom logic for retrieving the conditions array from cached_values.
154 * @param $cached_values
158 abstract protected function getConditions($cached_values);
161 * Custom logic for setting the conditions array in cached_values.
163 * @param $cached_values
166 * The conditions to set within the cached values.
169 * Return the $cached_values
171 abstract protected function setConditions($cached_values, $conditions);
174 * Custom logic for retrieving the contexts array from cached_values.
176 * @param $cached_values
178 * @return \Drupal\Core\Plugin\Context\ContextInterface[]
180 abstract protected function getContexts($cached_values);