EntityListBuilder.php 6.42 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\Core\Entity;

5 6
use Symfony\Component\DependencyInjection\ContainerInterface;

7
/**
8
 * Defines a generic implementation to build a listing of entities.
9 10
 *
 * @ingroup entity_api
11
 */
12
class EntityListBuilder extends EntityHandlerBase implements EntityListBuilderInterface, EntityHandlerInterface {
13 14

  /**
15
   * The entity storage class.
16
   *
17
   * @var \Drupal\Core\Entity\EntityStorageInterface
18 19 20 21
   */
  protected $storage;

  /**
22
   * The entity type ID.
23 24 25
   *
   * @var string
   */
26
  protected $entityTypeId;
27 28

  /**
29
   * Information about the entity type.
30
   *
31
   * @var \Drupal\Core\Entity\EntityTypeInterface
32
   */
33
  protected $entityType;
34

35
  /**
36
   * The number of entities to list per page, or FALSE to list all entities.
37
   *
38 39 40 41
   * For example, set this to FALSE if the list uses client-side filters that
   * require all entities to be listed (like the views overview).
   *
   * @var int|false
42 43 44
   */
  protected $limit = 50;

45 46 47
  /**
   * {@inheritdoc}
   */
48
  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
49
    return new static(
50
      $entity_type,
51
      $container->get('entity.manager')->getStorage($entity_type->id())
52 53 54
    );
  }

55
  /**
56
   * Constructs a new EntityListBuilder object.
57
   *
58 59
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type definition.
60 61
   * @param \Drupal\Core\Entity\EntityStorageInterface $storage
   *   The entity storage class.
62
   */
63
  public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage) {
64
    $this->entityTypeId = $entity_type->id();
65
    $this->storage = $storage;
66
    $this->entityType = $entity_type;
67 68 69
  }

  /**
70
   * {@inheritdoc}
71
   */
72
  public function getStorage() {
73 74 75 76
    return $this->storage;
  }

  /**
77
   * {@inheritdoc}
78 79
   */
  public function load() {
80 81 82 83 84
    $entity_ids = $this->getEntityIds();
    return $this->storage->loadMultiple($entity_ids);
  }

  /**
85
   * Loads entity IDs using a pager sorted by the entity id.
86 87 88 89 90
   *
   * @return array
   *   An array of entity IDs.
   */
  protected function getEntityIds() {
91 92 93 94 95 96 97 98
    $query = $this->getStorage()->getQuery()
      ->sort($this->entityType->getKey('id'));

    // Only add the pager if a limit is specified.
    if ($this->limit) {
      $query->pager($this->limit);
    }
    return $query->execute();
99 100
  }

101
  /**
102
   * Gets the label of an entity.
103 104 105 106 107
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity being listed.
   *
   * @return string
108 109 110 111 112
   *   The entity label.
   *
   * @deprecated in Drupal 8.0.x, will be removed before Drupal 9.0.0
   *   Use $entity->label() instead. This method used to escape the entity
   *   label. The render system's autoescape is now relied upon.
113 114
   */
  protected function getLabel(EntityInterface $entity) {
115
    return $entity->label();
116 117
  }

118
  /**
119
   * {@inheritdoc}
120 121
   */
  public function getOperations(EntityInterface $entity) {
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
    $operations = $this->getDefaultOperations($entity);
    $operations += $this->moduleHandler()->invokeAll('entity_operation', array($entity));
    $this->moduleHandler->alter('entity_operation', $operations, $entity);
    uasort($operations, '\Drupal\Component\Utility\SortArray::sortByWeightElement');

    return $operations;
  }

  /**
   * Gets this list's default operations.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity the operations are for.
   *
   * @return array
   *   The array structure is identical to the return value of
   *   self::getOperations().
   */
  protected function getDefaultOperations(EntityInterface $entity) {
141
    $operations = array();
142
    if ($entity->access('update') && $entity->hasLinkTemplate('edit-form')) {
143
      $operations['edit'] = array(
144
        'title' => $this->t('Edit'),
145
        'weight' => 10,
146 147
        'url' => $entity->urlInfo('edit-form'),
      );
148
    }
149
    if ($entity->access('delete') && $entity->hasLinkTemplate('delete-form')) {
150
      $operations['delete'] = array(
151
        'title' => $this->t('Delete'),
152
        'weight' => 100,
153 154
        'url' => $entity->urlInfo('delete-form'),
      );
155 156
    }

157 158 159 160 161 162 163 164 165
    return $operations;
  }

  /**
   * Builds the header row for the entity listing.
   *
   * @return array
   *   A render array structure of header strings.
   *
166
   * @see \Drupal\Core\Entity\EntityListBuilder::render()
167 168
   */
  public function buildHeader() {
169
    $row['operations'] = $this->t('Operations');
170 171 172 173 174 175
    return $row;
  }

  /**
   * Builds a row for an entity in the entity listing.
   *
176
   * @param \Drupal\Core\Entity\EntityInterface $entity
177 178 179 180 181
   *   The entity for this row of the list.
   *
   * @return array
   *   A render array structure of fields for this entity.
   *
182
   * @see \Drupal\Core\Entity\EntityListBuilder::render()
183 184
   */
  public function buildRow(EntityInterface $entity) {
185
    $row['operations']['data'] = $this->buildOperations($entity);
186 187 188 189 190 191
    return $row;
  }

  /**
   * Builds a renderable list of operation links for the entity.
   *
192
   * @param \Drupal\Core\Entity\EntityInterface $entity
193 194 195 196 197
   *   The entity on which the linked operations will be performed.
   *
   * @return array
   *   A renderable array of operation links.
   *
198
   * @see \Drupal\Core\Entity\EntityListBuilder::buildRow()
199 200 201
   */
  public function buildOperations(EntityInterface $entity) {
    $build = array(
202
      '#type' => 'operations',
203
      '#links' => $this->getOperations($entity),
204
    );
205

206 207 208 209
    return $build;
  }

  /**
210
   * {@inheritdoc}
211
   *
212
   * Builds the entity listing as renderable array for table.html.twig.
213 214 215 216
   *
   * @todo Add a link to add a new item to the #empty text.
   */
  public function render() {
217
    $build['table'] = array(
218
      '#type' => 'table',
219
      '#header' => $this->buildHeader(),
220
      '#title' => $this->getTitle(),
221
      '#rows' => array(),
222
      '#empty' => $this->t('There is no @label yet.', array('@label' => $this->entityType->getLabel())),
223 224
      '#cache' => [
        'contexts' => $this->entityType->getListCacheContexts(),
225
        'tags' => $this->entityType->getListCacheTags(),
226
      ],
227 228
    );
    foreach ($this->load() as $entity) {
229
      if ($row = $this->buildRow($entity)) {
230
        $build['table']['#rows'][$entity->id()] = $row;
231
      }
232
    }
233 234 235 236 237 238 239

    // Only add the pager if a limit is specified.
    if ($this->limit) {
      $build['pager'] = array(
        '#type' => 'pager',
      );
    }
240 241 242
    return $build;
  }

243
  /**
244
   * Gets the title of the page.
245 246 247 248 249 250 251 252
   *
   * @return string
   *   A string title of the page.
   */
  protected function getTitle() {
    return;
  }

253
}