Yaffs site version 1.1
[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\entityqueue\EntityQueueInterface;
7 use Symfony\Component\HttpFoundation\Request;
8 use Drupal\Core\Ajax\AjaxResponse;
9 use Drupal\Core\Ajax\ReplaceCommand;
10
11 /**
12  * Returns responses for Views UI routes.
13  */
14 class EntityQueueUIController extends ControllerBase {
15
16   /**
17    * Provides a list of all the subqueues of an entity queue.
18    *
19    * @param \Drupal\entityqueue\EntityQueueInterface $entity_queue
20    *   The entity queue.
21    *
22    * @return array
23    *   A render array.
24    */
25   public function subqueueList(EntityQueueInterface $entity_queue) {
26     $list_builder = $this->entityTypeManager()->getListBuilder('entity_subqueue');
27     $list_builder->setQueueId($entity_queue->id());
28
29     return $list_builder->render();
30   }
31
32   /**
33    * Returns a form to add a new subqeue.
34    *
35    * @param \Drupal\entityqueue\EntityQueueInterface $entity_queue
36    *   The queue this subqueue will be added to.
37    *
38    * @return array
39    *   The entity subqueue add form.
40    */
41   public function addForm(EntityQueueInterface $entity_queue) {
42     $subqueue = $this->entityTypeManager()->getStorage('entity_subqueue')->create(['queue' => $entity_queue->id()]);
43     return $this->entityFormBuilder()->getForm($subqueue);
44   }
45
46   /**
47    * Calls a method on an entity queue and reloads the listing page.
48    *
49    * @param \Drupal\entityqueue\EntityQueueInterface $entity_queue
50    *   The view being acted upon.
51    * @param string $op
52    *   The operation to perform, e.g., 'enable' or 'disable'.
53    * @param \Symfony\Component\HttpFoundation\Request $request
54    *   The current request.
55    *
56    * @return \Drupal\Core\Ajax\AjaxResponse|\Symfony\Component\HttpFoundation\RedirectResponse
57    *   Either returns a rebuilt listing page as an AJAX response, or redirects
58    *   back to the listing page.
59    */
60   public function ajaxOperation(EntityQueueInterface $entity_queue, $op, Request $request) {
61     // Perform the operation.
62     $entity_queue->$op()->save();
63
64     // If the request is via AJAX, return the rendered list as JSON.
65     if ($request->request->get('js')) {
66       $list = $this->entityTypeManager()->getListBuilder('entity_queue')->render();
67       $response = new AjaxResponse();
68       $response->addCommand(new ReplaceCommand('#entity-queue-list', $list));
69       return $response;
70     }
71
72     // Otherwise, redirect back to the page.
73     return $this->redirect('entity.entity_queue.collection');
74   }
75
76 }