00234cd9ab500766f9fe11cb7d1def448f0e5edf
[yaffs-website] / Form / ResolverRelationshipConfigure.php
1 <?php
2
3 namespace Drupal\ctools\Form;
4
5
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;
13
14 abstract class ResolverRelationshipConfigure extends FormBase {
15
16   /**
17    * @var \Drupal\user\SharedTempStoreFactory
18    */
19   protected $tempstore;
20
21   /**
22    * @var string
23    */
24   protected $tempstore_id;
25
26   /**
27    * @var string;
28    */
29   protected $machine_name;
30
31   /**
32    * {@inheritdoc}
33    */
34   public static function create(ContainerInterface $container) {
35     return new static($container->get('user.shared_tempstore'));
36   }
37
38   function __construct(SharedTempStoreFactory $tempstore) {
39     $this->tempstore = $tempstore;
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   public function getFormId() {
46     return 'ctools_context_configure';
47   }
48
49   /**
50    * {@inheritdoc}
51    */
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)) {
57       $id = $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.
64       $form['id'] = [
65         '#type' => 'value',
66         '#value' => $id
67       ];
68     }
69     else {
70       $label = '';
71       $machine_name = '';
72       $description = '';
73     }
74     $form['#attached']['library'][] = 'core/drupal.dialog.ajax';
75     $form['context'] = [
76       '#type' => 'value',
77       '#value' => $context
78     ];
79     $form['label'] = [
80       '#type' => 'textfield',
81       '#title' => $this->t('Label'),
82       '#default_value' => $label,
83       '#required' => TRUE,
84     ];
85     $form['machine_name'] = [
86       '#type' => 'textfield',
87       '#title' => $this->t('Machine Name'),
88       '#default_value' => $machine_name,
89       '#required' => TRUE,
90     ];
91     $form['description'] = [
92       '#type' => 'textarea',
93       '#title' => $this->t('Description'),
94       '#default_value' => $description,
95     ];
96     $form['submit'] = [
97       '#type' => 'submit',
98       '#value' => $this->t('Save'),
99       '#ajax' => [
100         'callback' => [$this, 'ajaxSave'],
101       ]
102     ];
103     return $form;
104   }
105
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.'));
112       }
113     }
114   }
115
116   /**
117    * {@inheritdoc}
118    */
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);
122     $context = [
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'),
127     ];
128     if ($form_state->hasValue('id')) {
129       $contexts[$form_state->getValue('id')] = $context;
130     }
131     else {
132       $contexts[] = $context;
133     }
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);
138   }
139
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());
146     return $response;
147   }
148
149   /**
150    * Document the route name and parameters for redirect after submission.
151    *
152    * @param $cached_values
153    *
154    * @return array
155    *   In the format of
156    *   return ['route.name', ['machine_name' => $this->machine_name, 'step' => 'step_name]];
157    */
158   abstract protected function getParentRouteInfo($cached_values);
159
160   /**
161    * Custom logic for retrieving the contexts array from cached_values.
162    *
163    * @param $cached_values
164    *
165    * @return array
166    */
167   abstract protected function getContexts($cached_values);
168
169   /**
170    * Custom logic for setting the contexts array in cached_values.
171    *
172    * @param $cached_values
173    *
174    * @param $contexts
175    *   The contexts to set within the cached values.
176    *
177    * @return mixed
178    *   Return the $cached_values
179    */
180   abstract protected function setContexts($cached_values, $contexts);
181
182 }