7dfff59825b57cfc214e9a8fa9febe0173597874
[yaffs-website] / web / modules / contrib / ctools / src / Form / ManageResolverRelationships.php
1 <?php
2
3 namespace Drupal\ctools\Form;
4
5 use Drupal\Component\Serialization\Json;
6 use Drupal\Core\Ajax\AjaxResponse;
7 use Drupal\Core\Ajax\OpenModalDialogCommand;
8 use Drupal\Core\Form\FormBase;
9 use Drupal\Core\Form\FormBuilderInterface;
10 use Drupal\Core\Form\FormStateInterface;
11 use Drupal\Core\Url;
12
13 abstract class ManageResolverRelationships extends FormBase {
14
15   /**
16    * @var string
17    */
18   protected $machine_name;
19
20   /**
21    * An array of property types that are eligible as relationships.
22    *
23    * @var array
24    */
25   protected $property_types = [];
26
27   /**
28    * {@inheritdoc}
29    */
30   public function getFormId() {
31     return 'ctools_manage_resolver_relationships_form';
32   }
33
34   /**
35    * {@inheritdoc}
36    */
37   public function buildForm(array $form, FormStateInterface $form_state) {
38     $cached_values = $form_state->getTemporaryValue('wizard');
39     $this->machine_name = $cached_values['id'];
40     $form['items'] = array(
41       '#type' => 'markup',
42       '#prefix' => '<div id="configured-relationships">',
43       '#suffix' => '</div>',
44       '#theme' => 'table',
45       '#header' => array($this->t('Context ID'), $this->t('Label'), $this->t('Data Type'), $this->t('Options')),
46       '#rows' => $this->renderRows($cached_values),
47       '#empty' => $this->t('No relationships have been added.')
48     );
49
50     $form['relationships'] = [
51       '#type' => 'select',
52       '#title' => $this->t('Add a relationship'),
53       '#options' => $this->getAvailableRelationships($cached_values),
54     ];
55     $form['add_relationship'] = [
56       '#type' => 'submit',
57       '#name' => 'add',
58       '#value' => $this->t('Add Relationship'),
59       '#ajax' => [
60         'callback' => [$this, 'addRelationship'],
61         'event' => 'click',
62       ],
63       '#submit' => [
64         'callback' => [$this, 'submitForm'],
65       ]
66     ];
67     return $form;
68   }
69
70   /**
71    * {@inheritdoc}
72    */
73   public function submitForm(array &$form, FormStateInterface $form_state) {
74     if ($form_state->getTriggeringElement()['#name'] == 'add') {
75       $cached_values = $form_state->getTemporaryValue('wizard');
76       list(, $route_parameters) = $this->getRelationshipOperationsRouteInfo($cached_values, $this->machine_name, $form_state->getValue('relationships'));
77       $form_state->setRedirect($this->getAddRoute($cached_values), $route_parameters);
78     }
79   }
80
81   public function addRelationship(array &$form, FormStateInterface $form_state) {
82     $relationship = $form_state->getValue('relationships');
83     $content = \Drupal::formBuilder()->getForm($this->getContextClass(), $relationship, $this->getTempstoreId(), $this->machine_name);
84     $content['#attached']['library'][] = 'core/drupal.dialog.ajax';
85     $cached_values = $form_state->getTemporaryValue('wizard');
86     list(, $route_parameters) = $this->getRelationshipOperationsRouteInfo($cached_values, $this->machine_name, $relationship);
87     $content['submit']['#attached']['drupalSettings']['ajax'][$content['submit']['#id']]['url'] = $this->url($this->getAddRoute($cached_values), $route_parameters, ['query' => [FormBuilderInterface::AJAX_FORM_REQUEST => TRUE]]);
88     $response = new AjaxResponse();
89     $response->addCommand(new OpenModalDialogCommand($this->t('Configure Relationship'), $content, array('width' => '700')));
90     return $response;
91   }
92
93   protected function getAvailableRelationships($cached_values) {
94     /** @var \Drupal\ctools\TypedDataResolver $resolver */
95     $resolver = \Drupal::service('ctools.typed_data.resolver');
96     return $resolver->getTokensForContexts($this->getContexts($cached_values));
97   }
98
99   /**
100    * @param $cached_values
101    *
102    * @return array
103    */
104   protected function renderRows($cached_values) {
105     $contexts = array();
106     foreach ($this->getContexts($cached_values) as $row => $context) {
107       list($route_name, $route_parameters) = $this->getRelationshipOperationsRouteInfo($cached_values, $this->machine_name, $row);
108       $build = array(
109         '#type' => 'operations',
110         '#links' => $this->getOperations($cached_values, $row, $route_name, $route_parameters),
111       );
112       $contexts[$row] = array(
113         $row,
114         $context->getContextDefinition()->getLabel(),
115         $context->getContextDefinition()->getDataType(),
116         'operations' => [
117           'data' => $build,
118         ],
119       );
120     }
121     return $contexts;
122   }
123
124   /**
125    * @param array $cached_values
126    * @param string $row
127    * @param string $route_name_base
128    * @param array $route_parameters
129    *
130    * @return mixed
131    */
132   protected function getOperations($cached_values, $row, $route_name_base, array $route_parameters = array()) {
133     // Base contexts will not be a : separated and generated relationships should have 3 parts.
134     if (count(explode(':', $row)) < 2) {
135       return [];
136     }
137     $operations['edit'] = array(
138       'title' => $this->t('Edit'),
139       'url' => new Url($route_name_base . '.edit', $route_parameters),
140       'weight' => 10,
141       'attributes' => array(
142         'class' => ['use-ajax'],
143         'data-dialog-type' => 'modal',
144         'data-dialog-options' => Json::encode([
145           'width' => 700,
146         ]),
147       ),
148     );
149     $route_parameters['id'] = $route_parameters['context'];
150     $operations['delete'] = array(
151       'title' => $this->t('Delete'),
152       'url' => new Url($route_name_base . '.delete', $route_parameters),
153       'weight' => 100,
154       'attributes' => array(
155         'class' => array('use-ajax'),
156         'data-dialog-type' => 'modal',
157         'data-dialog-options' => Json::encode([
158           'width' => 700,
159         ]),
160       ),
161     );
162     return $operations;
163   }
164
165   /**
166    * Return a subclass of '\Drupal\ctools\Form\ResolverRelationshipConfigure'.
167    *
168    * The ConditionConfigure class is designed to be subclassed with custom
169    * route information to control the modal/redirect needs of your use case.
170    *
171    * @return string
172    */
173   abstract protected function getContextClass($cached_values);
174
175   /**
176    * The route to which relationship 'add' actions should submit.
177    *
178    * @return string
179    */
180   abstract protected function getAddRoute($cached_values);
181
182   /**
183    * Provide the tempstore id for your specified use case.
184    *
185    * @return string
186    */
187   abstract protected function getTempstoreId();
188
189   /**
190    * @param $cached_values
191    *
192    * @return \Drupal\Core\Plugin\Context\ContextInterface[]
193    */
194   abstract protected function getContexts($cached_values);
195
196   /**
197    * @param string $cached_values
198    * @param string $machine_name
199    * @param string $row
200    *
201    * @return array
202    */
203   abstract protected function getRelationshipOperationsRouteInfo($cached_values, $machine_name, $row);
204
205 }