EntityListController.php 5.32 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\Core\Entity\EntityListController.
6 7 8 9
 */

namespace Drupal\Core\Entity;

10 11
use Drupal\Core\Extension\ModuleHandlerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
12
use Drupal\Component\Utility\String;
13

14 15 16
/**
 * Provides a generic implementation of an entity list controller.
 */
17
class EntityListController implements EntityListControllerInterface, EntityControllerInterface {
18 19 20 21

  /**
   * The entity storage controller class.
   *
22
   * @var \Drupal\Core\Entity\EntityStorageControllerInterface
23 24 25
   */
  protected $storage;

26 27 28 29 30 31 32
  /**
   * The module handler to invoke hooks on.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
  /**
   * The entity type name.
   *
   * @var string
   */
  protected $entityType;

  /**
   * The entity info array.
   *
   * @var array
   *
   * @see entity_get_info()
   */
  protected $entityInfo;

49 50 51 52 53 54 55 56 57 58 59 60
  /**
   * {@inheritdoc}
   */
  public static function createInstance(ContainerInterface $container, $entity_type, array $entity_info) {
    return new static(
      $entity_type,
      $entity_info,
      $container->get('plugin.manager.entity')->getStorageController($entity_type),
      $container->get('module_handler')
    );
  }

61 62 63
  /**
   * Constructs a new EntityListController object.
   *
64
   * @param string $entity_type
65
   *   The type of entity to be listed.
66 67 68
   * @param array $entity_info
   *   An array of entity info for the entity type.
   * @param \Drupal\Core\Entity\EntityStorageControllerInterface $storage
69
   *   The entity storage controller class.
70 71
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler to invoke hooks on.
72
   */
73
  public function __construct($entity_type, array $entity_info, EntityStorageControllerInterface $storage, ModuleHandlerInterface $module_handler) {
74 75
    $this->entityType = $entity_type;
    $this->storage = $storage;
76 77
    $this->entityInfo = $entity_info;
    $this->moduleHandler = $module_handler;
78 79 80
  }

  /**
81
   * Implements \Drupal\Core\Entity\EntityListControllerInterface::getStorageController().
82 83 84 85 86 87
   */
  public function getStorageController() {
    return $this->storage;
  }

  /**
88
   * Implements \Drupal\Core\Entity\EntityListControllerInterface::load().
89 90
   */
  public function load() {
91
    return $this->storage->loadMultiple();
92 93
  }

94 95 96 97 98 99 100 101 102 103 104 105 106
  /**
   * Returns the escaped label of an entity.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity being listed.
   *
   * @return string
   *   The escaped entity label.
   */
  protected function getLabel(EntityInterface $entity) {
    return String::checkPlain($entity->label());
  }

107
  /**
108
   * {@inheritdoc}
109 110 111
   */
  public function getOperations(EntityInterface $entity) {
    $uri = $entity->uri();
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130

    $operations = array();
    if ($entity->access('update')) {
      $operations['edit'] = array(
        'title' => t('Edit'),
        'href' => $uri['path'] . '/edit',
        'options' => $uri['options'],
        'weight' => 10,
      );
    }
    if ($entity->access('delete')) {
      $operations['delete'] = array(
        'title' => t('Delete'),
        'href' => $uri['path'] . '/delete',
        'options' => $uri['options'],
        'weight' => 100,
      );
    }

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
    return $operations;
  }

  /**
   * Builds the header row for the entity listing.
   *
   * @return array
   *   A render array structure of header strings.
   *
   * @see Drupal\Core\Entity\EntityListController::render()
   */
  public function buildHeader() {
    $row['operations'] = t('Operations');
    return $row;
  }

  /**
   * Builds a row for an entity in the entity listing.
   *
150
   * @param \Drupal\Core\Entity\EntityInterface $entity
151 152 153 154 155 156 157 158
   *   The entity for this row of the list.
   *
   * @return array
   *   A render array structure of fields for this entity.
   *
   * @see Drupal\Core\Entity\EntityListController::render()
   */
  public function buildRow(EntityInterface $entity) {
159
    $row['operations']['data'] = $this->buildOperations($entity);
160 161 162 163 164 165
    return $row;
  }

  /**
   * Builds a renderable list of operation links for the entity.
   *
166
   * @param \Drupal\Core\Entity\EntityInterface $entity
167 168 169 170 171 172 173 174 175 176
   *   The entity on which the linked operations will be performed.
   *
   * @return array
   *   A renderable array of operation links.
   *
   * @see Drupal\Core\Entity\EntityListController::render()
   */
  public function buildOperations(EntityInterface $entity) {
    // Retrieve and sort operations.
    $operations = $this->getOperations($entity);
177
    $this->moduleHandler->alter('entity_operation', $operations, $entity);
178 179
    uasort($operations, 'drupal_sort_weight');
    $build = array(
180
      '#type' => 'operations',
181 182 183 184 185 186
      '#links' => $operations,
    );
    return $build;
  }

  /**
187
   * Implements \Drupal\Core\Entity\EntityListControllerInterface::render().
188 189 190 191 192 193 194 195 196 197 198 199 200
   *
   * Builds the entity list as renderable array for theme_table().
   *
   * @todo Add a link to add a new item to the #empty text.
   */
  public function render() {
    $build = array(
      '#theme' => 'table',
      '#header' => $this->buildHeader(),
      '#rows' => array(),
      '#empty' => t('There is no @label yet.', array('@label' => $this->entityInfo['label'])),
    );
    foreach ($this->load() as $entity) {
201 202 203
      if ($row = $this->buildRow($entity)) {
        $build['#rows'][$entity->id()] = $row;
      }
204 205 206 207 208
    }
    return $build;
  }

}