EntityListController.php 3.82 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
<?php

/**
 * @file
 * Definition of Drupal\views_ui_listing\EntityListController.
 */

namespace Drupal\views_ui_listing;

use Drupal\Core\Entity\EntityInterface;

/**
 * Provides a generic implementation of an entity list controller.
 */
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.
   */
  public function __construct($entity_type) {
    $this->entityType = $entity_type;
    $this->storage = entity_get_controller($this->entityType);
    $this->entityInfo = entity_get_info($this->entityType);
  }

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

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

  /**
   * Implements Drupal\views_ui_listing\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;
  }

  /**
   * Retrieves the entity list path from the entity information.
   *
   * @return string
   *   The internal system path where the entity list will be rendered.
   *
   * @todo What is this method for, other than fetching the list path? Is this
   *  for http://drupal.org/node/1783964 ? Should it be on the interface?
   */
  public function getPath() {
    return $this->entityInfo['list path'];
  }

  /**
   * Implements Drupal\views_ui_listing\EntityListControllerInterface::buildHeader().
   */
  public function buildHeader() {
    $row['label'] = t('Label');
    $row['id'] = t('Machine name');
    $row['operations'] = t('Operations');
    return $row;
  }

  /**
   * Implements Drupal\views_ui_listing\EntityListControllerInterface::buildRow().
   */
  public function buildRow(EntityInterface $entity) {
    $row['label'] = $entity->label();
    $row['id'] = $entity->id();
    $operations = $this->buildOperations($entity);
    $row['operations'] = drupal_render($operations);
    return $row;
  }

  /**
   * Implements Drupal\views_ui_listing\EntityListControllerInterface::buildOperations().
   */
  public function buildOperations(EntityInterface $entity) {
    // Retrieve and sort operations.
    $operations = $this->getOperations($entity);
    uasort($operations, 'drupal_sort_weight');
    $build = array(
      '#theme' => 'links',
      '#links' => $operations,
    );
    return $build;
  }

  /**
   * Implements Drupal\views_ui_listing\EntityListControllerInterface::render().
   */
  public function render() {
    $build = array(
      '#theme' => 'table',
      '#header' => $this->buildHeader(),
      '#rows' => array(),
      '#empty' => t('There is no @label yet. <a href="@add-url">Add one</a>.', array(
        '@label' => $this->entityInfo['label'],
        '@add-url' => url($this->getPath() . '/add'),
      )),
    );
    foreach ($this->load() as $entity) {
      $build['#rows'][$entity->id()] = $this->buildRow($entity);
    }
    return $build;
  }

}