Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / entityqueue / src / Controller / EntityQueueUIController.php
1 <?php
2
3 namespace Drupal\entityqueue\Controller;
4
5 use Drupal\Core\Controller\ControllerBase;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Routing\RouteMatch;
8 use Drupal\entityqueue\EntityQueueInterface;
9 use Symfony\Component\HttpFoundation\Request;
10 use Drupal\Core\Ajax\AjaxResponse;
11 use Drupal\Core\Ajax\ReplaceCommand;
12 use Drupal\entityqueue\EntitySubqueueInterface;
13 use Drupal\Core\Url;
14 use Drupal\Core\Routing\RouteMatchInterface;
15 use Drupal\Core\Access\AccessResult;
16
17 /**
18  * Returns responses for Entityqueue UI routes.
19  */
20 class EntityQueueUIController extends ControllerBase {
21
22   /**
23    * Provides a list of all the subqueues of an entity queue.
24    *
25    * @param \Drupal\entityqueue\EntityQueueInterface $entity_queue
26    *   The entity queue.
27    *
28    * @return array
29    *   A render array.
30    */
31   public function subqueueList(EntityQueueInterface $entity_queue) {
32     $list_builder = $this->entityTypeManager()->getListBuilder('entity_subqueue');
33     $list_builder->setQueueId($entity_queue->id());
34
35     return $list_builder->render();
36   }
37
38   /**
39    * Provides a list of subqueues where an entity can be added.
40    *
41    * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
42    *   The route match.
43    * @param string $entity_type_id
44    *   (optional) The entity type ID.
45    * @param \Drupal\Core\Entity\EntityInterface $entity
46    *   (optional) An entity object.
47    *
48    * @return array
49    *   Array of page elements to render.
50    */
51   public function subqueueListForEntity(RouteMatchInterface $route_match, $entity_type_id = NULL, EntityInterface $entity = NULL) {
52     if (!$entity) {
53       $entity = $route_match->getParameter($entity_type_id);
54     }
55
56     $queues = $this->getAvailableQueuesForEntity($entity);
57     $subqueues = $this->entityTypeManager()->getStorage('entity_subqueue')->loadByProperties(['queue' => array_keys($queues)]);
58     $list_builder = $this->entityTypeManager()->getListBuilder('entity_subqueue');
59
60     $build['#title'] = $this->t('Entityqueues for %title', ['%title' => $entity->label()]);
61     $build['#type'] = 'container';
62     $build['#attributes']['id'] = 'entity-subqueue-list';
63     $build['#attached']['library'][] = 'core/drupal.ajax';
64     $build['table'] = [
65       '#type' => 'table',
66       '#header' => $list_builder->buildHeader(),
67       '#rows' => [],
68       '#cache' => [],
69       '#empty' => $this->t('There are no queues available.'),
70     ];
71
72     /** @var \Drupal\entityqueue\EntitySubqueueInterface $subqueue */
73     foreach ($subqueues as $subqueue_id => $subqueue) {
74       $row = $list_builder->buildRow($subqueue);
75
76       // Check if entity is in queue
77       $subqueue_items = $subqueue->get('items')->getValue();
78       if (in_array($entity->id(), array_column($subqueue_items, 'target_id'), TRUE)) {
79         $row['operations']['data']['#links'] = [
80           'remove-item' => [
81             'title' => $this->t('Remove from queue'),
82             'url' => Url::fromRoute('entity.entity_subqueue.remove_item', ['entity_queue' => $queues[$subqueue->bundle()]->id(), 'entity_subqueue' => $subqueue_id, 'entity' => $entity->id()]),
83             'attributes' => [
84               'class' => ['use-ajax'],
85             ],
86           ],
87         ];
88       }
89       else {
90         $row['operations']['data']['#links'] = [
91           'add-item' => [
92             'title' => $this->t('Add to queue'),
93             'url' => Url::fromRoute('entity.entity_subqueue.add_item', ['entity_queue' => $queues[$subqueue->bundle()]->id(), 'entity_subqueue' => $subqueue_id, 'entity' => $entity->id()]),
94             'attributes' => [
95               'class' => ['use-ajax'],
96             ],
97           ],
98         ];
99       }
100
101       // Add an operation for editing the subqueue items.
102       // First, compute the destination to send the user back to the
103       // entityqueue tab they're currently on. We can't rely on <current>
104       // since if any of the AJAX links are used and the page is rebuilt,
105       // <current> will point to the most recent AJAX callback, not the
106       // original entityqueue tab.
107       $destination = Url::fromRoute("entity.$entity_type_id.entityqueue", [$entity_type_id => $entity->id()])->toString();
108       $row['operations']['data']['#links']['edit-subqueue-items'] = [
109         'title' => $this->t('Edit subqueue items'),
110         'url' => $subqueue->toUrl('edit-form', ['query' => ['destination' => $destination]]),
111       ];
112
113       $build['table']['#rows'][$subqueue->id()] = $row;
114     }
115
116     return $build;
117   }
118
119   /**
120    * Returns a form to add a new subqeue.
121    *
122    * @param \Drupal\entityqueue\EntityQueueInterface $entity_queue
123    *   The queue this subqueue will be added to.
124    *
125    * @return array
126    *   The entity subqueue add form.
127    */
128   public function addForm(EntityQueueInterface $entity_queue) {
129     $subqueue = $this->entityTypeManager()->getStorage('entity_subqueue')->create(['queue' => $entity_queue->id()]);
130     return $this->entityFormBuilder()->getForm($subqueue);
131   }
132
133   /**
134    * Calls a method on an entity queue and reloads the listing page.
135    *
136    * @param \Drupal\entityqueue\EntityQueueInterface $entity_queue
137    *   The view being acted upon.
138    * @param string $op
139    *   The operation to perform, e.g., 'enable' or 'disable'.
140    * @param \Symfony\Component\HttpFoundation\Request $request
141    *   The current request.
142    *
143    * @return \Drupal\Core\Ajax\AjaxResponse|\Symfony\Component\HttpFoundation\RedirectResponse
144    *   Either returns a rebuilt listing page as an AJAX response, or redirects
145    *   back to the listing page.
146    */
147   public function ajaxOperation(EntityQueueInterface $entity_queue, $op, Request $request) {
148     // Perform the operation.
149     $entity_queue->$op()->save();
150
151     // If the request is via AJAX, return the rendered list as JSON.
152     if ($request->request->get('js')) {
153       $list = $this->entityTypeManager()->getListBuilder('entity_queue')->render();
154       $response = new AjaxResponse();
155       $response->addCommand(new ReplaceCommand('#entity-queue-list', $list));
156       return $response;
157     }
158
159     // Otherwise, redirect back to the page.
160     return $this->redirect('entity.entity_queue.collection');
161   }
162
163   /**
164    * Calls a method on an entity subqueue page and reloads the page.
165    *
166    * @param \Drupal\entityqueue\EntitySubqueueInterface $entity_subqueue
167    *   The subqueue being acted upon.
168    * @param string $op
169    *   The operation to perform, e.g., 'addItem' or 'removeItem'.
170    * @param \Symfony\Component\HttpFoundation\Request $request
171    *   The current request.
172    *
173    * @return \Drupal\Core\Ajax\AjaxResponse|\Symfony\Component\HttpFoundation\RedirectResponse
174    *   Either returns a rebuilt listing page as an AJAX response, or redirects
175    *   back to the current page.
176    */
177   public function subqueueAjaxOperation(EntitySubqueueInterface $entity_subqueue, $op, Request $request) {
178     $entity_id = $request->get('entity');
179     $entity = $this->entityTypeManager()->getStorage($entity_subqueue->getQueue()->getTargetEntityTypeId())->load($entity_id);
180
181     // Perform the operation.
182     $entity_subqueue->$op($entity)->save();
183
184     // If the request is via AJAX, return the rendered list as JSON.
185     if ($request->request->get('js')) {
186       $route_match = RouteMatch::createFromRequest($request);
187       $list = $this->subqueueListForEntity($route_match, $entity->getEntityTypeId(), $entity);
188       $response = new AjaxResponse();
189       $response->addCommand(new ReplaceCommand('#entity-subqueue-list', $list));
190       return $response;
191     }
192
193     // Otherwise, redirect back to the page.
194     return $this->redirect('<current>');
195   }
196
197   /**
198    * Checks access for a specific request.
199    *
200    * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
201    *   The route match.
202    * @param string $entity_type_id
203    *   (optional) The entity type ID.
204    *
205    * @return \Drupal\Core\Access\AccessResultInterface
206    *   The access result.
207    */
208   public function access(RouteMatchInterface $route_match, $entity_type_id = NULL) {
209     /** @var \Drupal\Core\Entity\EntityInterface $entity */
210     $entity = $route_match->getParameter($entity_type_id);
211
212     if ($this->getAvailableQueuesForEntity($entity)) {
213       return AccessResult::allowed();
214     }
215
216     return AccessResult::forbidden();
217   }
218
219   /**
220    * Gets a list of queues which can hold this entity.
221    *
222    * @param \Drupal\Core\Entity\EntityInterface $entity
223    *   An entity object.
224    *
225    * @return \Drupal\entityqueue\EntityQueueInterface[]
226    *   An array of entity queues which can hold this entity.
227    */
228   protected function getAvailableQueuesForEntity(EntityInterface $entity) {
229     $storage = $this->entityTypeManager()->getStorage('entity_queue');
230
231     $queue_ids = $storage->getQuery()
232       ->condition('entity_settings.target_type', $entity->getEntityTypeId(), '=')
233       ->condition('status', TRUE)
234       ->execute();
235
236     $queues = $storage->loadMultiple($queue_ids);
237     $queues = array_filter($queues, function ($queue) use ($entity) {
238       /** @var \Drupal\entityqueue\EntityQueueInterface $queue */
239       $queue_settings = $queue->getEntitySettings();
240       $target_bundles = &$queue_settings['handler_settings']['target_bundles'];
241       return ($target_bundles === NULL || in_array($entity->bundle(), $target_bundles, TRUE));
242     });
243
244     return $queues;
245   }
246
247 }