3 namespace Drupal\entityqueue;
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;
15 * Defines a class that builds a listing of entity queues.
17 class EntityQueueListBuilder extends ConfigEntityListBuilder {
22 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
24 protected $entityTypeManager;
29 protected $limit = FALSE;
32 * Constructs a new class instance.
34 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
35 * The entity type definition.
36 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
39 public function __construct(EntityTypeInterface $entity_type, EntityTypeManagerInterface $entity_type_manager) {
40 parent::__construct($entity_type, $entity_type_manager->getStorage($entity_type->id()));
42 $this->entityTypeManager = $entity_type_manager;
48 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
51 $container->get('entity_type.manager')
58 public function load() {
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')) {
69 if ($entity->status()) {
70 $entities['enabled'][] = $entity;
73 $entities['disabled'][] = $entity;
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');
88 return $header + parent::buildHeader();
94 public function buildRow(EntityInterface $entity) {
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()]),
111 public function render() {
112 $entities = $this->load();
114 $build['#type'] = 'container';
115 $build['#attributes']['id'] = 'entity-queue-list';
116 $build['#attached']['library'][] = 'core/drupal.ajax';
118 'contexts' => Cache::mergeContexts($this->entityType->getListCacheContexts(), ['user.permissions']),
119 'tags' => $this->entityType->getListCacheTags(),
122 $build['enabled']['heading']['#markup'] = '<h2>' . $this->t('Enabled', [], ['context' => 'Plural']) . '</h2>';
123 $build['disabled']['heading']['#markup'] = '<h2>' . $this->t('Disabled', [], ['context' => 'Plural']) . '</h2>';
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'] = [
131 'class' => ['entity-queue-listing-table'],
133 '#header' => $this->buildHeader(),
136 'contexts' => $this->entityType->getListCacheContexts(),
137 'tags' => $this->entityType->getListCacheTags(),
140 foreach ($entities[$status] as $entity) {
141 $build[$status]['table']['#rows'][$entity->id()] = $this->buildRow($entity);
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.');
155 public function getDefaultOperations(EntityInterface $entity) {
156 $operations = parent::getDefaultOperations($entity);
158 if (isset($operations['edit'])) {
159 $operations['edit']['title'] = $this->t('Configure');
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';
171 // Allow queue handlers to add their own operations.
172 $operations += $entity->getHandlerPlugin()->getQueueListBuilderOperations();
178 * Returns the number of items in a subqueue or the number of subqueues.
180 * @param \Drupal\entityqueue\EntityQueueInterface $queue
181 * An entity queue object.
184 * The number of items in a subqueue or the number of subqueues.
186 protected function getQueueItemsStatus(EntityQueueInterface $queue) {
187 $handler = $queue->getHandlerPlugin();
190 if ($handler->supportsMultipleSubqueues()) {
191 $subqueues_count = $this->entityTypeManager->getStorage('entity_subqueue')->getQuery()
192 ->condition('queue', $queue->id(), '=')
196 $items = $this->t('@count subqueues', ['@count' => $subqueues_count]);
199 $subqueue = EntitySubqueue::load($queue->id());
201 $items = $this->t('@count items', ['@count' => count($subqueue->items)]);