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\Form\FormBase;
10 use Drupal\Core\Form\FormStateInterface;
11 use Drupal\user\SharedTempStoreFactory;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
14 abstract class ResolverRelationshipConfigure extends FormBase {
17 * @var \Drupal\user\SharedTempStoreFactory
24 protected $tempstore_id;
29 protected $machine_name;
34 public static function create(ContainerInterface $container) {
35 return new static($container->get('user.shared_tempstore'));
38 function __construct(SharedTempStoreFactory $tempstore) {
39 $this->tempstore = $tempstore;
45 public function getFormId() {
46 return 'ctools_context_configure';
52 public function buildForm(array $form, FormStateInterface $form_state, $context = NULL, $tempstore_id = NULL, $machine_name = NULL) {
53 $this->tempstore_id = $tempstore_id;
54 $this->machine_name = $machine_name;
55 $cached_values = $this->tempstore->get($this->tempstore_id)->get($this->machine_name);
56 if (is_numeric($context)) {
58 $contexts = $this->getContexts($cached_values);
59 $context = $contexts[$id]['context'];
60 $label = $contexts[$id]['label'];
61 $machine_name = $contexts[$id]['machine_name'];
62 $description = $contexts[$id]['description'];
63 // Conditionally set this form element so that we can update or add.
74 $form['#attached']['library'][] = 'core/drupal.dialog.ajax';
80 '#type' => 'textfield',
81 '#title' => $this->t('Label'),
82 '#default_value' => $label,
85 $form['machine_name'] = [
86 '#type' => 'textfield',
87 '#title' => $this->t('Machine Name'),
88 '#default_value' => $machine_name,
91 $form['description'] = [
92 '#type' => 'textarea',
93 '#title' => $this->t('Description'),
94 '#default_value' => $description,
98 '#value' => $this->t('Save'),
100 'callback' => [$this, 'ajaxSave'],
106 public function validateForm(array &$form, FormStateInterface $form_state) {
107 $machine_name = $form_state->getValue('machine_name');
108 $cached_values = $this->tempstore->get($this->tempstore_id)->get($this->machine_name);
109 foreach ($this->getContexts($cached_values) as $id => $context) {
110 if ($context['machine_name'] == $machine_name) {
111 $form_state->setError($form['machine_name'], $this->t('That machine name is in use by another context definition.'));
119 public function submitForm(array &$form, FormStateInterface $form_state) {
120 $cached_values = $this->tempstore->get($this->tempstore_id)->get($this->machine_name);
121 $contexts = $this->getContexts($cached_values);
123 'context' => $form_state->getValue('context'),
124 'label' => $form_state->getValue('label'),
125 'machine_name' => $form_state->getValue('machine_name'),
126 'description' => $form_state->getValue('description'),
128 if ($form_state->hasValue('id')) {
129 $contexts[$form_state->getValue('id')] = $context;
132 $contexts[] = $context;
134 $cached_values = $this->setContexts($cached_values, $contexts);
135 $this->tempstore->get($this->tempstore_id)->set($this->machine_name, $cached_values);
136 list($route_name, $route_parameters) = $this->getParentRouteInfo($cached_values);
137 $form_state->setRedirect($route_name, $route_parameters);
140 public function ajaxSave(array &$form, FormStateInterface $form_state) {
141 $response = new AjaxResponse();
142 $cached_values = $this->tempstore->get($this->tempstore_id)->get($this->machine_name);
143 list($route_name, $route_parameters) = $this->getParentRouteInfo($cached_values);
144 $response->addCommand(new RedirectCommand($this->url($route_name, $route_parameters)));
145 $response->addCommand(new CloseModalDialogCommand());
150 * Document the route name and parameters for redirect after submission.
152 * @param $cached_values
156 * return ['route.name', ['machine_name' => $this->machine_name, 'step' => 'step_name]];
158 abstract protected function getParentRouteInfo($cached_values);
161 * Custom logic for retrieving the contexts array from cached_values.
163 * @param $cached_values
167 abstract protected function getContexts($cached_values);
170 * Custom logic for setting the contexts array in cached_values.
172 * @param $cached_values
175 * The contexts to set within the cached values.
178 * Return the $cached_values
180 abstract protected function setContexts($cached_values, $contexts);