EntityDisplayModeListController.php 4.29 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
<?php

/**
 * @file
 * Contains \Drupal\entity\EntityDisplayModeListController.
 */

namespace Drupal\entity;

use Drupal\Core\Config\Entity\ConfigEntityListController;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageControllerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides the listing for entity display modes.
 */
class EntityDisplayModeListController extends ConfigEntityListController {

  /**
   * The entity info for all entity types.
   *
   * @var array
   */
  protected $entityInfoComplete;

  /**
   * Constructs a new EntityListController object.
   *
   * @param string $entity_type
   *   The type of entity to be listed.
   * @param array $entity_info
   *   An array of entity info for the entity type.
   * @param \Drupal\Core\Entity\EntityStorageControllerInterface $storage
   *   The entity storage controller class.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler to invoke hooks on.
   * @param array $entity_info_complete
   *   The entity info for all entity types.
   */
  public function __construct($entity_type, array $entity_info, EntityStorageControllerInterface $storage, ModuleHandlerInterface $module_handler, array $entity_info_complete) {
    parent::__construct($entity_type, $entity_info, $storage, $module_handler);

    $this->entityInfoComplete = $entity_info_complete;
  }

  /**
   * {@inheritdoc}
   */
  public static function createInstance(ContainerInterface $container, $entity_type, array $entity_info) {
    $entity_manager = $container->get('plugin.manager.entity');
    return new static(
      $entity_type,
      $entity_info,
      $entity_manager->getStorageController($entity_type),
      $container->get('module_handler'),
      $entity_manager->getDefinitions()
    );
  }

  /**
   * {@inheritdoc}
   */
  public function buildHeader() {
66 67
    $header['label'] = t('Label');
    return $header + parent::buildHeader();
68 69 70 71 72 73
  }

  /**
   * {@inheritdoc}
   */
  public function buildRow(EntityInterface $entity) {
74 75
    $row['label'] = $this->getLabel($entity);
    return $row + parent::buildRow($entity);
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
  }

  /**
   * {@inheritdoc}
   */
  public function load() {
    $entities = array();
    foreach (parent::load() as $entity) {
      $entities[$entity->getTargetType()][] = $entity;
    }
    return $entities;
  }

  /**
   * {@inheritdoc}
   */
  public function render() {
    $build = array();
    foreach ($this->load() as $entity_type => $entities) {
      if (!isset($this->entityInfoComplete[$entity_type])) {
        continue;
      }

      // Filter entities
      if ($this->entityInfoComplete[$entity_type]['fieldable'] && !$this->isValidEntity($entity_type)) {
        continue;
      }

      $table = array(
        '#prefix' => '<h2>' . $this->entityInfoComplete[$entity_type]['label'] . '</h2>',
        '#type' => 'table',
        '#header' => $this->buildHeader(),
        '#rows' => array(),
      );
      foreach ($entities as $entity) {
        if ($row = $this->buildRow($entity)) {
          $table['#rows'][$entity->id()] = $row;
        }
      }

      // Move content at the top.
      if ($entity_type == 'node') {
        $table['#weight'] = -10;
      }

      $short_type = str_replace('_mode', '', $this->entityType);
      $table['#rows']['_add_new'][] = array(
        'data' => array(
          '#type' => 'link',
          '#href' => "admin/structure/display-modes/$short_type/add/$entity_type",
          '#title' => t('Add new %label @entity-type', array('%label' => $this->entityInfoComplete[$entity_type]['label'], '@entity-type' => strtolower($this->entityInfo['label']))),
          '#options' => array(
            'html' => TRUE,
          ),
        ),
        'colspan' => count($table['#header']),
      );
      $build[$entity_type] = $table;
    }
    return $build;
  }

  /**
   * Filters entities based on their controllers.
   *
   * @param $entity_type
   *   The entity type of the entity that needs to be validated.
   *
   * @return bool
   *   TRUE if the entity has the correct controller, FALSE if the entity
   *   doesn't has the correct controller.
   */
  protected function isValidEntity($entity_type) {
    return isset($this->entityInfoComplete[$entity_type]['controllers']['render']);
  }

}