3 namespace Drupal\ctools\Form;
6 use Drupal\Core\Ajax\AjaxResponse;
7 use Drupal\Core\Ajax\CloseModalDialogCommand;
8 use Drupal\Core\Ajax\RedirectCommand;
9 use Drupal\Core\Entity\Entity;
10 use Drupal\Core\Entity\Plugin\DataType\EntityAdapter;
11 use Drupal\Core\Form\FormBase;
12 use Drupal\Core\Form\FormStateInterface;
13 use Drupal\Core\Plugin\Context\Context;
14 use Drupal\Core\Plugin\Context\ContextDefinition;
15 use Drupal\Core\Plugin\Context\ContextInterface;
17 use Drupal\user\SharedTempStoreFactory;
18 use Symfony\Component\DependencyInjection\ContainerInterface;
20 abstract class ContextConfigure extends FormBase {
23 * @var \Drupal\user\SharedTempStoreFactory
30 protected $tempstore_id;
35 protected $machine_name;
40 public static function create(ContainerInterface $container) {
41 return new static($container->get('user.shared_tempstore'));
44 function __construct(SharedTempStoreFactory $tempstore) {
45 $this->tempstore = $tempstore;
51 public function getFormId() {
52 return 'ctools_context_configure';
58 public function buildForm(array $form, FormStateInterface $form_state, $context_id = NULL, $tempstore_id = NULL, $machine_name = NULL) {
59 $this->tempstore_id = $tempstore_id;
60 $this->machine_name = $machine_name;
61 $cached_values = $this->tempstore->get($this->tempstore_id)->get($this->machine_name);
62 $contexts = $this->getContexts($cached_values);
64 if (!empty($contexts[$context_id])) {
65 $context = $contexts[$context_id];
66 $machine_name = $context_id;
70 $context_definition = new ContextDefinition($context_id);
71 $context = new Context($context_definition);
74 $label = $context->getContextDefinition()->getLabel();
75 $description = $context->getContextDefinition()->getDescription();
76 $data_type = $context->getContextDefinition()->getDataType();
77 $form['#attached']['library'][] = 'core/drupal.dialog.ajax';
78 $form['context_id'] = [
80 '#value' => $context_id
83 '#type' => 'textfield',
84 '#title' => $this->t('Label'),
85 '#default_value' => $label,
88 $form['machine_name'] = [
89 '#type' => 'machine_name',
90 '#title' => $this->t('Machine Name'),
91 '#default_value' => $machine_name,
95 'source' => ['label'],
96 'exists' => [$this, 'contextExists'],
98 '#disabled' => $this->disableMachineName($cached_values, $machine_name),
100 $form['description'] = [
101 '#type' => 'textarea',
102 '#title' => $this->t('Description'),
103 '#default_value' => $description,
105 if (strpos($data_type, 'entity:') === 0) {
106 list(, $entity_type) = explode(':', $data_type);
107 /** @var EntityAdapter $entity */
108 $entity = $edit ? $context->getContextValue() : NULL;
109 $form['context_value'] = [
110 '#type' => 'entity_autocomplete',
112 '#target_type' => $entity_type,
113 '#default_value' => $entity,
114 '#title' => $this->t('Select entity'),
118 $value = $context->getContextData()->getValue();
119 $form['context_value'] = [
120 '#title' => $this->t('Set a context value'),
121 '#type' => 'textfield',
123 '#default_value' => $value,
128 '#value' => $this->t('Save'),
130 'callback' => [$this, 'ajaxSave'],
139 public function validateForm(array &$form, FormStateInterface $form_state) {
140 // If these are not equal, then we're adding a new context and should not override an existing context.
141 if ($form_state->getValue('machine_name') != $form_state->getValue('context_id')) {
142 $machine_name = $form_state->getValue('machine_name');
143 $cached_values = $this->tempstore->get($this->tempstore_id)->get($this->machine_name);
144 if (!empty($this->getContexts($cached_values)[$machine_name])) {
145 $form_state->setError($form['machine_name'], $this->t('That machine name is in use by another context definition.'));
153 public function submitForm(array &$form, FormStateInterface $form_state) {
154 $cached_values = $this->tempstore->get($this->tempstore_id)->get($this->machine_name);
155 $contexts = $this->getContexts($cached_values);
156 if ($form_state->getValue('machine_name') != $form_state->getValue('context_id')) {
157 $data_type = $form_state->getValue('context_id');
158 $context_definition = new ContextDefinition($data_type, $form_state->getValue('label'), TRUE, FALSE, $form_state->getValue('description'));
161 $context = $contexts[$form_state->getValue('machine_name')];
162 $context_definition = $context->getContextDefinition();
163 $context_definition->setLabel($form_state->getValue('label'));
164 $context_definition->setDescription($form_state->getValue('description'));
166 // We're dealing with an entity and should make sure it's loaded.
167 if (strpos($context_definition->getDataType(), 'entity:') === 0) {
168 list(, $entity_type) = explode(':', $context_definition->getDataType());
169 if (is_numeric($form_state->getValue('context_value'))) {
170 $value = \Drupal::entityTypeManager()->getStorage($entity_type)->load($form_state->getValue('context_value'));
173 // No loading required for non-entity values.
175 $value = $form_state->getValue('context_value');
177 $context = new Context($context_definition, $value);
179 $cached_values = $this->addContext($cached_values, $form_state->getValue('machine_name'), $context);
180 $this->tempstore->get($this->tempstore_id)->set($this->machine_name, $cached_values);
181 list($route_name, $route_parameters) = $this->getParentRouteInfo($cached_values);
182 $form_state->setRedirect($route_name, $route_parameters);
185 public function ajaxSave(array &$form, FormStateInterface $form_state) {
186 $response = new AjaxResponse();
187 $cached_values = $this->tempstore->get($this->tempstore_id)->get($this->machine_name);
188 list($route_name, $route_parameters) = $this->getParentRouteInfo($cached_values);
189 $url = new Url($route_name, $route_parameters);
190 $response->addCommand(new RedirectCommand($url->toString()));
191 $response->addCommand(new CloseModalDialogCommand());
196 * Document the route name and parameters for redirect after submission.
198 * @param $cached_values
202 * return ['route.name', ['machine_name' => $this->machine_name, 'step' => 'step_name]];
204 abstract protected function getParentRouteInfo($cached_values);
207 * Custom logic for retrieving the contexts array from cached_values.
209 * @param $cached_values
211 * @return \Drupal\Core\Plugin\Context\ContextInterface[]
213 abstract protected function getContexts($cached_values);
216 * Custom logic for adding a context to the cached_values contexts array.
218 * @param array $cached_values
219 * The cached_values currently in use.
220 * @param string $context_id
221 * The context identifier.
222 * @param \Drupal\Core\Plugin\Context\ContextInterface $context
223 * The context to add or update within the cached values.
226 * Return the $cached_values
228 abstract protected function addContext($cached_values, $context_id, ContextInterface $context);
231 * Custom "exists" logic for the context to be created.
233 * @param string $value
234 * The name of the context.
236 * The machine_name element
237 * @param \Drupal\Core\Form\FormStateInterface $form_state
241 * Return true if a context of this name exists.
243 abstract public function contextExists($value, $element, $form_state);
246 * Determines if the machine_name should be disabled.
248 * @param $cached_values
252 abstract protected function disableMachineName($cached_values, $machine_name);