class EntityListController

Provides a generic implementation of an entity list controller.

Hierarchy

Expanded class hierarchy of EntityListController

2 files declare their use of EntityListController
ConfigEntityListController.php in drupal/core/lib/Drupal/Core/Config/Entity/ConfigEntityListController.php
Definition of Drupal\Core\Config\Entity\ConfigEntityListController.
ViewListController.php in drupal/core/modules/views/views_ui/lib/Drupal/views_ui/ViewListController.php
Definition of Drupal\views_ui\ViewListController.

File

drupal/core/lib/Drupal/Core/Entity/EntityListController.php, line 13
Definition of Drupal\Core\Entity\EntityListController.

Namespace

Drupal\Core\Entity
View source
class EntityListController implements EntityListControllerInterface {

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

  /**
   * The entity type name.
   *
   * @var string
   */
  protected $entityType;

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

  /**
   * Constructs a new EntityListController object.
   *
   * @param string $entity_type.
   *   The type of entity to be listed.
   * @param Drupal\Core\Entity\EntityStorageControllerInterface $storage.
   *   The entity storage controller class.
   */
  public function __construct($entity_type, EntityStorageControllerInterface $storage) {
    $this->entityType = $entity_type;
    $this->storage = $storage;
    $this->entityInfo = entity_get_info($this->entityType);
  }

  /**
   * Implements Drupal\Core\Entity\EntityListControllerInterface::getStorageController().
   */
  public function getStorageController() {
    return $this->storage;
  }

  /**
   * Implements Drupal\Core\Entity\EntityListControllerInterface::load().
   */
  public function load() {
    return $this->storage
      ->load();
  }

  /**
   * Implements Drupal\Core\Entity\EntityListControllerInterface::getOperations().
   */
  public function getOperations(EntityInterface $entity) {
    $uri = $entity
      ->uri();
    $operations['edit'] = array(
      'title' => t('Edit'),
      'href' => $uri['path'] . '/edit',
      'options' => $uri['options'],
      'weight' => 10,
    );
    $operations['delete'] = array(
      'title' => t('Delete'),
      'href' => $uri['path'] . '/delete',
      'options' => $uri['options'],
      'weight' => 100,
    );
    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['label'] = t('Label');
    $row['id'] = t('Machine name');
    $row['operations'] = t('Operations');
    return $row;
  }

  /**
   * Builds a row for an entity in the entity listing.
   *
   * @param Drupal\Core\Entity\EntityInterface $entity
   *   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) {
    $row['label'] = $entity
      ->label();
    $row['id'] = $entity
      ->id();
    $operations = $this
      ->buildOperations($entity);
    $row['operations']['data'] = $operations;
    return $row;
  }

  /**
   * Builds a renderable list of operation links for the entity.
   *
   * @param Drupal\Core\Entity\EntityInterface $entity
   *   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);
    uasort($operations, 'drupal_sort_weight');
    $build = array(
      '#type' => 'operations',
      '#links' => $operations,
    );
    return $build;
  }

  /**
   * Implements Drupal\Core\Entity\EntityListControllerInterface::render().
   *
   * 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) {
      $build['#rows'][$entity
        ->id()] = $this
        ->buildRow($entity);
    }
    return $build;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityListController::$entityInfo protected property The entity info array.
EntityListController::$entityType protected property The entity type name.
EntityListController::$storage protected property The entity storage controller class.
EntityListController::buildHeader public function Builds the header row for the entity listing. 2
EntityListController::buildOperations public function Builds a renderable list of operation links for the entity. 1
EntityListController::buildRow public function Builds a row for an entity in the entity listing. 2
EntityListController::getOperations public function Implements Drupal\Core\Entity\EntityListControllerInterface::getOperations(). Overrides EntityListControllerInterface::getOperations 2
EntityListController::getStorageController public function Implements Drupal\Core\Entity\EntityListControllerInterface::getStorageController(). Overrides EntityListControllerInterface::getStorageController
EntityListController::load public function Implements Drupal\Core\Entity\EntityListControllerInterface::load(). Overrides EntityListControllerInterface::load 2
EntityListController::render public function Implements Drupal\Core\Entity\EntityListControllerInterface::render(). Overrides EntityListControllerInterface::render 1
EntityListController::__construct public function Constructs a new EntityListController object.