EntityDisplayModeListBuilder.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace Drupal\field_ui;
  3. use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
  4. use Drupal\Core\Entity\EntityInterface;
  5. use Drupal\Core\Entity\EntityStorageInterface;
  6. use Drupal\Core\Entity\EntityTypeInterface;
  7. use Drupal\Core\Url;
  8. use Symfony\Component\DependencyInjection\ContainerInterface;
  9. /**
  10. * Defines a class to build a listing of view mode entities.
  11. *
  12. * @see \Drupal\Core\Entity\Entity\EntityViewMode
  13. */
  14. class EntityDisplayModeListBuilder extends ConfigEntityListBuilder {
  15. /**
  16. * All entity types.
  17. *
  18. * @var \Drupal\Core\Entity\EntityTypeInterface[]
  19. */
  20. protected $entityTypes;
  21. /**
  22. * Constructs a new EntityDisplayModeListBuilder object.
  23. *
  24. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  25. * The entity type definition.
  26. * @param \Drupal\Core\Entity\EntityStorageInterface $storage
  27. * The entity storage class.
  28. * @param \Drupal\Core\Entity\EntityTypeInterface[] $entity_types
  29. * List of all entity types.
  30. */
  31. public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, array $entity_types) {
  32. parent::__construct($entity_type, $storage);
  33. // Override the default limit (50) in order to display all view modes.
  34. $this->limit = FALSE;
  35. $this->entityTypes = $entity_types;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
  41. $entity_manager = $container->get('entity.manager');
  42. return new static(
  43. $entity_type,
  44. $entity_manager->getStorage($entity_type->id()),
  45. $entity_manager->getDefinitions()
  46. );
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function buildHeader() {
  52. $header['label'] = $this->t('Name');
  53. return $header + parent::buildHeader();
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function buildRow(EntityInterface $entity) {
  59. $row['label'] = $entity->label();
  60. return $row + parent::buildRow($entity);
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function load() {
  66. $entities = [];
  67. foreach (parent::load() as $entity) {
  68. $entities[$entity->getTargetType()][] = $entity;
  69. }
  70. return $entities;
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function render() {
  76. $build = [];
  77. foreach ($this->load() as $entity_type => $entities) {
  78. if (!isset($this->entityTypes[$entity_type])) {
  79. continue;
  80. }
  81. // Filter entities.
  82. if (!$this->isValidEntity($entity_type)) {
  83. continue;
  84. }
  85. $table = [
  86. '#prefix' => '<h2>' . $this->entityTypes[$entity_type]->getLabel() . '</h2>',
  87. '#type' => 'table',
  88. '#header' => $this->buildHeader(),
  89. '#rows' => [],
  90. ];
  91. foreach ($entities as $entity) {
  92. if ($row = $this->buildRow($entity)) {
  93. $table['#rows'][$entity->id()] = $row;
  94. }
  95. }
  96. // Move content at the top.
  97. if ($entity_type == 'node') {
  98. $table['#weight'] = -10;
  99. }
  100. $short_type = str_replace(['entity_', '_mode'], '', $this->entityTypeId);
  101. $table['#rows']['_add_new'][] = [
  102. 'data' => [
  103. '#type' => 'link',
  104. '#url' => Url::fromRoute($short_type == 'view' ? 'entity.entity_view_mode.add_form' : 'entity.entity_form_mode.add_form', ['entity_type_id' => $entity_type]),
  105. '#title' => $this->t('Add new %label @entity-type', ['%label' => $this->entityTypes[$entity_type]->getLabel(), '@entity-type' => $this->entityType->getLowercaseLabel()]),
  106. ],
  107. 'colspan' => count($table['#header']),
  108. ];
  109. $build[$entity_type] = $table;
  110. }
  111. return $build;
  112. }
  113. /**
  114. * Filters entities based on their view builder handlers.
  115. *
  116. * @param $entity_type
  117. * The entity type of the entity that needs to be validated.
  118. *
  119. * @return bool
  120. * TRUE if the entity has the correct view builder handler, FALSE if the
  121. * entity doesn't have the correct view builder handler.
  122. */
  123. protected function isValidEntity($entity_type) {
  124. return $this->entityTypes[$entity_type]->get('field_ui_base_route') && $this->entityTypes[$entity_type]->hasViewBuilderClass();
  125. }
  126. }