Pathologic was missing because of a .git folder inside.
[yaffs-website] / web / modules / contrib / entityqueue / src / EntityQueueListBuilder.php
1 <?php
2
3 namespace Drupal\entityqueue;
4
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;
11
12 /**
13  * Defines a class that builds a listing of entity queues.
14  */
15 class EntityQueueListBuilder extends ConfigEntityListBuilder {
16
17   /**
18    * The entity manager.
19    *
20    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
21    */
22   protected $entityTypeManager;
23
24   /**
25    * Constructs a new class instance.
26    *
27    * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
28    *   The entity type definition.
29    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
30    *   The entity manager.
31    */
32   public function __construct(EntityTypeInterface $entity_type, EntityTypeManagerInterface $entity_type_manager) {
33     parent::__construct($entity_type, $entity_type_manager->getStorage($entity_type->id()));
34
35     $this->entityTypeManager = $entity_type_manager;
36   }
37
38   /**
39    * {@inheritdoc}
40    */
41   public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
42     return new static(
43       $entity_type,
44       $container->get('entity_type.manager')
45     );
46   }
47
48   /**
49    * {@inheritdoc}
50    */
51   public function load() {
52     $entities = array(
53       'enabled' => array(),
54       'disabled' => array(),
55     );
56     foreach (parent::load() as $entity) {
57       if ($entity->status()) {
58         $entities['enabled'][] = $entity;
59       }
60       else {
61         $entities['disabled'][] = $entity;
62       }
63     }
64     return $entities;
65   }
66
67   /**
68    * {@inheritdoc}
69    */
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');
75
76     return $header + parent::buildHeader();
77   }
78
79   /**
80    * {@inheritdoc}
81    */
82   public function buildRow(EntityInterface $entity) {
83     $row = [
84       'data' => [
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())),
91     ];
92
93     return $row;
94   }
95
96   /**
97    * {@inheritdoc}
98    */
99   public function render() {
100     $entities = $this->load();
101
102     $build['#type'] = 'container';
103     $build['#attributes']['id'] = 'entity-queue-list';
104     $build['#attached']['library'][] = 'core/drupal.ajax';
105
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>';
108
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(
113         '#type' => 'table',
114         '#attributes' => array(
115           'class' => array('entity-queue-listing-table'),
116         ),
117         '#header' => $this->buildHeader(),
118         '#rows' => array(),
119         '#cache' => [
120           'contexts' => $this->entityType->getListCacheContexts(),
121           'tags' => $this->entityType->getListCacheTags(),
122         ],
123       );
124       foreach ($entities[$status] as $entity) {
125         $build[$status]['table']['#rows'][$entity->id()] = $this->buildRow($entity);
126       }
127     }
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.');
132
133     return $build;
134   }
135
136   /**
137    * {@inheritdoc}
138    */
139   public function getDefaultOperations(EntityInterface $entity) {
140     $operations = parent::getDefaultOperations($entity);
141
142     $operations['edit']['title'] = $this->t('Configure');
143
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';
150       }
151     }
152
153     // Allow queue handlers to add their own operations.
154     $operations += $entity->getHandlerPlugin()->getQueueListBuilderOperations();
155
156     return $operations;
157   }
158
159   /**
160    * Returns the number of items in a subqueue or the number of subqueues.
161    *
162    * @param \Drupal\entityqueue\EntityQueueInterface $queue
163    *   An entity queue object.
164    *
165    * @return string
166    *   The number of items in a subqueue or the number of subqueues.
167    */
168   protected function getQueueItemsStatus(EntityQueueInterface $queue) {
169     $handler = $queue->getHandlerPlugin();
170
171     $items = NULL;
172     if ($handler->supportsMultipleSubqueues()) {
173       $subqueues_count = $this->entityTypeManager->getStorage('entity_subqueue')->getQuery()
174         ->condition('queue', $queue->id(), '=')
175         ->count()
176         ->execute();
177
178       $items = $this->t('@count subqueues', ['@count' => $subqueues_count]);
179     }
180     else {
181       $subqueue = EntitySubqueue::load($queue->id());
182
183       $items = $this->t('@count items', ['@count' => count($subqueue->items)]);
184     }
185
186     return $items;
187   }
188
189 }