ee506a89db31b68717214a6f5ed7fc2f3005bf35
[yaffs-website] / Form / ManageConditions.php
1 <?php
2
3 namespace Drupal\ctools\Form;
4
5
6 use Drupal\Component\Plugin\PluginManagerInterface;
7 use Drupal\Component\Serialization\Json;
8 use Drupal\Core\Ajax\AjaxResponse;
9 use Drupal\Core\Ajax\OpenModalDialogCommand;
10 use Drupal\Core\Form\FormBase;
11 use Drupal\Core\Form\FormBuilderInterface;
12 use Drupal\Core\Form\FormStateInterface;
13 use Drupal\Core\Url;
14 use Symfony\Component\DependencyInjection\ContainerInterface;
15
16 abstract class ManageConditions extends FormBase {
17
18   /**
19    * @var \Drupal\Core\Condition\ConditionManager
20    */
21   protected $manager;
22
23   /**
24    * @var string
25    */
26   protected $machine_name;
27
28   public static function create(ContainerInterface $container) {
29     return new static($container->get('plugin.manager.condition'));
30   }
31
32   function __construct(PluginManagerInterface $manager) {
33     $this->manager = $manager;
34   }
35
36   /**
37    * {@inheritdoc}
38    */
39   public function getFormId() {
40     return 'ctools_manage_conditions_form';
41   }
42
43   /**
44    * {@inheritdoc}
45    */
46   public function buildForm(array $form, FormStateInterface $form_state) {
47     $cached_values = $form_state->getTemporaryValue('wizard');
48     $this->machine_name = $cached_values['id'];
49     $form['#attached']['library'][] = 'core/drupal.dialog.ajax';
50     $options = [];
51     $contexts = $this->getContexts($cached_values);
52     foreach ($this->manager->getDefinitionsForContexts($contexts) as $plugin_id => $definition) {
53       $options[$plugin_id] = (string) $definition['label'];
54     }
55     $form['items'] = array(
56       '#type' => 'markup',
57       '#prefix' => '<div id="configured-conditions">',
58       '#suffix' => '</div>',
59       '#theme' => 'table',
60       '#header' => array($this->t('Plugin Id'), $this->t('Summary'), $this->t('Operations')),
61       '#rows' => $this->renderRows($cached_values),
62       '#empty' => $this->t('No required conditions have been configured.')
63     );
64     $form['conditions'] = [
65       '#type' => 'select',
66       '#options' => $options,
67     ];
68     $form['add'] = [
69       '#type' => 'submit',
70       '#name' => 'add',
71       '#value' => $this->t('Add Condition'),
72       '#ajax' => [
73         'callback' => [$this, 'add'],
74         'event' => 'click',
75       ],
76       '#submit' => [
77         'callback' => [$this, 'submitForm'],
78       ]
79     ];
80     return $form;
81   }
82
83   /**
84    * {@inheritdoc}
85    */
86   public function submitForm(array &$form, FormStateInterface $form_state) {
87     $cached_values = $form_state->getTemporaryValue('wizard');
88     list(, $route_parameters) = $this->getOperationsRouteInfo($cached_values, $this->machine_name, $form_state->getValue('conditions'));
89     $form_state->setRedirect($this->getAddRoute($cached_values), $route_parameters);
90   }
91
92   public function add(array &$form, FormStateInterface $form_state) {
93     $condition = $form_state->getValue('conditions');
94     $content = \Drupal::formBuilder()->getForm($this->getConditionClass(), $condition, $this->getTempstoreId(), $this->machine_name);
95     $content['#attached']['library'][] = 'core/drupal.dialog.ajax';
96     $cached_values = $form_state->getTemporaryValue('wizard');
97     list(, $route_parameters) = $this->getOperationsRouteInfo($cached_values, $this->machine_name, $form_state->getValue('conditions'));
98     $content['submit']['#attached']['drupalSettings']['ajax'][$content['submit']['#id']]['url'] = $this->url($this->getAddRoute($cached_values), $route_parameters, ['query' => [FormBuilderInterface::AJAX_FORM_REQUEST => TRUE]]);
99     $response = new AjaxResponse();
100     $response->addCommand(new OpenModalDialogCommand($this->t('Configure Required Context'), $content, array('width' => '700')));
101     return $response;
102   }
103
104   /**
105    * @param $cached_values
106    *
107    * @return array
108    */
109   public function renderRows($cached_values) {
110     $configured_conditions = array();
111     foreach ($this->getConditions($cached_values) as $row => $condition) {
112       /** @var $instance \Drupal\Core\Condition\ConditionInterface */
113       $instance = $this->manager->createInstance($condition['id'], $condition);
114       list($route_name, $route_parameters) = $this->getOperationsRouteInfo($cached_values, $cached_values['id'], $row);
115       $build = array(
116         '#type' => 'operations',
117         '#links' => $this->getOperations($route_name, $route_parameters),
118       );
119       $configured_conditions[] = array(
120         $instance->getPluginId(),
121         $instance->summary(),
122         'operations' => [
123           'data' => $build,
124         ],
125       );
126     }
127     return $configured_conditions;
128   }
129
130   protected function getOperations($route_name_base, array $route_parameters = array()) {
131     $operations['edit'] = array(
132       'title' => $this->t('Edit'),
133       'url' => new Url($route_name_base . '.edit', $route_parameters),
134       'weight' => 10,
135       'attributes' => array(
136         'class' => ['use-ajax'],
137         'data-dialog-type' => 'modal',
138         'data-dialog-options' => Json::encode([
139           'width' => 700,
140         ]),
141       ),
142     );
143     $route_parameters['id'] = $route_parameters['condition'];
144     $operations['delete'] = array(
145       'title' => $this->t('Delete'),
146       'url' => new Url($route_name_base . '.delete', $route_parameters),
147       'weight' => 100,
148       'attributes' => array(
149         'class' => array('use-ajax'),
150         'data-dialog-type' => 'modal',
151         'data-dialog-options' => Json::encode([
152           'width' => 700,
153         ]),
154       ),
155     );
156     return $operations;
157   }
158
159   /**
160    * Return a subclass of '\Drupal\ctools\Form\ConditionConfigure'.
161    *
162    * The ConditionConfigure class is designed to be subclassed with custom
163    * route information to control the modal/redirect needs of your use case.
164    *
165    * @return string
166    */
167   abstract protected function getConditionClass();
168
169   /**
170    * The route to which condition 'add' actions should submit.
171    *
172    * @param mixed $cached_values
173    *
174    * @return string
175    */
176   abstract protected function getAddRoute($cached_values);
177
178   /**
179    * Provide the tempstore id for your specified use case.
180    *
181    * @return string
182    */
183   abstract protected function getTempstoreId();
184
185   /**
186    * Document the route name and parameters for edit/delete context operations.
187    *
188    * The route name returned from this method is used as a "base" to which
189    * ".edit" and ".delete" are appeneded in the getOperations() method.
190    * Subclassing '\Drupal\ctools\Form\ConditionConfigure' and
191    * '\Drupal\ctools\Form\ConditionDelete' should set you up for using this
192    * approach quite seamlessly.
193    *
194    * @param mixed $cached_values
195    *
196    * @param string $machine_name
197    *
198    * @param string $row
199    *
200    * @return array
201    *   In the format of
202    *   return ['route.base.name', ['machine_name' => $machine_name, 'context' => $row]];
203    */
204   abstract protected function getOperationsRouteInfo($cached_values, $machine_name, $row);
205
206   /**
207    * Custom logic for retrieving the conditions array from cached_values.
208    *
209    * @param $cached_values
210    *
211    * @return array
212    */
213   abstract protected function getConditions($cached_values);
214
215   /**
216    * Custom logic for retrieving the contexts array from cached_values.
217    *
218    * @param $cached_values
219    *
220    * @return \Drupal\Core\Plugin\Context\ContextInterface[]
221    */
222   abstract protected function getContexts($cached_values);
223
224 }