3 namespace Drupal\entityqueue;
5 use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Entity\EntityTypeInterface;
8 use Drupal\Core\Entity\EntityTypeManagerInterface;
9 use Drupal\entityqueue\Entity\EntitySubqueue;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
13 * Defines a class that builds a listing of entity queues.
15 class EntityQueueListBuilder extends ConfigEntityListBuilder {
20 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
22 protected $entityTypeManager;
25 * Constructs a new class instance.
27 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
28 * The entity type definition.
29 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
32 public function __construct(EntityTypeInterface $entity_type, EntityTypeManagerInterface $entity_type_manager) {
33 parent::__construct($entity_type, $entity_type_manager->getStorage($entity_type->id()));
35 $this->entityTypeManager = $entity_type_manager;
41 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
44 $container->get('entity_type.manager')
51 public function load() {
54 'disabled' => array(),
56 foreach (parent::load() as $entity) {
57 if ($entity->status()) {
58 $entities['enabled'][] = $entity;
61 $entities['disabled'][] = $entity;
70 public function buildHeader() {
71 $header['label'] = $this->t('Queue name');
72 $header['target_type'] = $this->t('Target type');
73 $header['handler'] = $this->t('Queue type');
74 $header['items'] = $this->t('Items');
76 return $header + parent::buildHeader();
82 public function buildRow(EntityInterface $entity) {
85 'label' => $entity->label(),
86 'target_type' => $this->entityTypeManager->getDefinition($entity->getTargetEntityTypeId())->getLabel(),
87 'handler' => $entity->getHandlerPlugin()->getPluginDefinition()['title'],
88 'items' => $this->getQueueItemsStatus($entity),
89 ] + parent::buildRow($entity),
90 'title' => $this->t('Machine name: @name', array('@name' => $entity->id())),
99 public function render() {
100 $entities = $this->load();
102 $build['#type'] = 'container';
103 $build['#attributes']['id'] = 'entity-queue-list';
104 $build['#attached']['library'][] = 'core/drupal.ajax';
106 $build['enabled']['heading']['#markup'] = '<h2>' . $this->t('Enabled', array(), array('context' => 'Plural')) . '</h2>';
107 $build['disabled']['heading']['#markup'] = '<h2>' . $this->t('Disabled', array(), array('context' => 'Plural')) . '</h2>';
109 foreach (array('enabled', 'disabled') as $status) {
110 $build[$status]['#type'] = 'container';
111 $build[$status]['#attributes'] = array('class' => array('entity-queue-list-section', $status));
112 $build[$status]['table'] = array(
114 '#attributes' => array(
115 'class' => array('entity-queue-listing-table'),
117 '#header' => $this->buildHeader(),
120 'contexts' => $this->entityType->getListCacheContexts(),
121 'tags' => $this->entityType->getListCacheTags(),
124 foreach ($entities[$status] as $entity) {
125 $build[$status]['table']['#rows'][$entity->id()] = $this->buildRow($entity);
128 // @todo Use a placeholder for the entity label if this is abstracted to
129 // other entity types.
130 $build['enabled']['table']['#empty'] = $this->t('There are no enabled queues.');
131 $build['disabled']['table']['#empty'] = $this->t('There are no disabled queues.');
139 public function getDefaultOperations(EntityInterface $entity) {
140 $operations = parent::getDefaultOperations($entity);
142 $operations['edit']['title'] = $this->t('Configure');
144 // Add AJAX functionality to enable/disable operations.
145 foreach (array('enable', 'disable') as $op) {
146 if (isset($operations[$op])) {
147 $operations[$op]['url'] = $entity->toUrl($op);
148 // Enable and disable operations should use AJAX.
149 $operations[$op]['attributes']['class'][] = 'use-ajax';
153 // Allow queue handlers to add their own operations.
154 $operations += $entity->getHandlerPlugin()->getQueueListBuilderOperations();
160 * Returns the number of items in a subqueue or the number of subqueues.
162 * @param \Drupal\entityqueue\EntityQueueInterface $queue
163 * An entity queue object.
166 * The number of items in a subqueue or the number of subqueues.
168 protected function getQueueItemsStatus(EntityQueueInterface $queue) {
169 $handler = $queue->getHandlerPlugin();
172 if ($handler->supportsMultipleSubqueues()) {
173 $subqueues_count = $this->entityTypeManager->getStorage('entity_subqueue')->getQuery()
174 ->condition('queue', $queue->id(), '=')
178 $items = $this->t('@count subqueues', ['@count' => $subqueues_count]);
181 $subqueue = EntitySubqueue::load($queue->id());
183 $items = $this->t('@count items', ['@count' => count($subqueue->items)]);