Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / entityqueue / src / EntityQueueListBuilder.php
1 <?php
2
3 namespace Drupal\entityqueue;
4
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
7 use Drupal\Core\Entity\EntityInterface;
8 use Drupal\Core\Entity\EntityTypeInterface;
9 use Drupal\Core\Entity\EntityTypeManagerInterface;
10 use Drupal\Core\Session\AccountInterface;
11 use Drupal\entityqueue\Entity\EntitySubqueue;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13
14 /**
15  * Defines a class that builds a listing of entity queues.
16  */
17 class EntityQueueListBuilder extends ConfigEntityListBuilder {
18
19   /**
20    * The entity manager.
21    *
22    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
23    */
24   protected $entityTypeManager;
25
26   /**
27    * {@inheritdoc}
28    */
29   protected $limit = FALSE;
30
31   /**
32    * Constructs a new class instance.
33    *
34    * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
35    *   The entity type definition.
36    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
37    *   The entity manager.
38    */
39   public function __construct(EntityTypeInterface $entity_type, EntityTypeManagerInterface $entity_type_manager) {
40     parent::__construct($entity_type, $entity_type_manager->getStorage($entity_type->id()));
41
42     $this->entityTypeManager = $entity_type_manager;
43   }
44
45   /**
46    * {@inheritdoc}
47    */
48   public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
49     return new static(
50       $entity_type,
51       $container->get('entity_type.manager')
52     );
53   }
54
55   /**
56    * {@inheritdoc}
57    */
58   public function load() {
59     $entities = [
60       'enabled' => [],
61       'disabled' => [],
62     ];
63     /** @var \Drupal\entityqueue\EntityQueueInterface $entity */
64     foreach (parent::load() as $entity) {
65       // Don't display queues which can not be edited by the user.
66       if (!$entity->access('update')) {
67         continue;
68       }
69       if ($entity->status()) {
70         $entities['enabled'][] = $entity;
71       }
72       else {
73         $entities['disabled'][] = $entity;
74       }
75     }
76     return $entities;
77   }
78
79   /**
80    * {@inheritdoc}
81    */
82   public function buildHeader() {
83     $header['label'] = $this->t('Queue name');
84     $header['target_type'] = $this->t('Target type');
85     $header['handler'] = $this->t('Queue type');
86     $header['items'] = $this->t('Items');
87
88     return $header + parent::buildHeader();
89   }
90
91   /**
92    * {@inheritdoc}
93    */
94   public function buildRow(EntityInterface $entity) {
95     $row = [
96       'data' => [
97         'label' => $entity->label(),
98         'target_type' => $this->entityTypeManager->getDefinition($entity->getTargetEntityTypeId())->getLabel(),
99         'handler' => $entity->getHandlerPlugin()->getPluginDefinition()['title'],
100         'items' => $this->getQueueItemsStatus($entity),
101       ] + parent::buildRow($entity),
102       'title' => $this->t('Machine name: @name', ['@name' => $entity->id()]),
103     ];
104
105     return $row;
106   }
107
108   /**
109    * {@inheritdoc}
110    */
111   public function render() {
112     $entities = $this->load();
113
114     $build['#type'] = 'container';
115     $build['#attributes']['id'] = 'entity-queue-list';
116     $build['#attached']['library'][] = 'core/drupal.ajax';
117     $build['#cache'] = [
118       'contexts' => Cache::mergeContexts($this->entityType->getListCacheContexts(), ['user.permissions']),
119       'tags' => $this->entityType->getListCacheTags(),
120     ];
121
122     $build['enabled']['heading']['#markup'] = '<h2>' . $this->t('Enabled', [], ['context' => 'Plural']) . '</h2>';
123     $build['disabled']['heading']['#markup'] = '<h2>' . $this->t('Disabled', [], ['context' => 'Plural']) . '</h2>';
124
125     foreach (['enabled', 'disabled'] as $status) {
126       $build[$status]['#type'] = 'container';
127       $build[$status]['#attributes'] = ['class' => ['entity-queue-list-section', $status]];
128       $build[$status]['table'] = [
129         '#type' => 'table',
130         '#attributes' => [
131           'class' => ['entity-queue-listing-table'],
132         ],
133         '#header' => $this->buildHeader(),
134         '#rows' => [],
135         '#cache' => [
136           'contexts' => $this->entityType->getListCacheContexts(),
137           'tags' => $this->entityType->getListCacheTags(),
138         ],
139       ];
140       foreach ($entities[$status] as $entity) {
141         $build[$status]['table']['#rows'][$entity->id()] = $this->buildRow($entity);
142       }
143     }
144     // @todo Use a placeholder for the entity label if this is abstracted to
145     // other entity types.
146     $build['enabled']['table']['#empty'] = $this->t('There are no enabled queues.');
147     $build['disabled']['table']['#empty'] = $this->t('There are no disabled queues.');
148
149     return $build;
150   }
151
152   /**
153    * {@inheritdoc}
154    */
155   public function getDefaultOperations(EntityInterface $entity) {
156     $operations = parent::getDefaultOperations($entity);
157
158     if (isset($operations['edit'])) {
159       $operations['edit']['title'] = $this->t('Configure');
160     }
161
162     // Add AJAX functionality to enable/disable operations.
163     foreach (['enable', 'disable'] as $op) {
164       if (isset($operations[$op])) {
165         $operations[$op]['url'] = $entity->toUrl($op);
166         // Enable and disable operations should use AJAX.
167         $operations[$op]['attributes']['class'][] = 'use-ajax';
168       }
169     }
170
171     // Allow queue handlers to add their own operations.
172     $operations += $entity->getHandlerPlugin()->getQueueListBuilderOperations();
173
174     return $operations;
175   }
176
177   /**
178    * Returns the number of items in a subqueue or the number of subqueues.
179    *
180    * @param \Drupal\entityqueue\EntityQueueInterface $queue
181    *   An entity queue object.
182    *
183    * @return string
184    *   The number of items in a subqueue or the number of subqueues.
185    */
186   protected function getQueueItemsStatus(EntityQueueInterface $queue) {
187     $handler = $queue->getHandlerPlugin();
188
189     $items = NULL;
190     if ($handler->supportsMultipleSubqueues()) {
191       $subqueues_count = $this->entityTypeManager->getStorage('entity_subqueue')->getQuery()
192         ->condition('queue', $queue->id(), '=')
193         ->count()
194         ->execute();
195
196       $items = $this->t('@count subqueues', ['@count' => $subqueues_count]);
197     }
198     else {
199       $subqueue = EntitySubqueue::load($queue->id());
200
201       $items = $this->t('@count items', ['@count' => count($subqueue->items)]);
202     }
203
204     return $items;
205   }
206
207 }