ActionListBuilder.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace Drupal\action;
  3. use Drupal\Core\Action\ActionManager;
  4. use Drupal\Core\Entity\EntityInterface;
  5. use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
  6. use Drupal\Core\Entity\EntityStorageInterface;
  7. use Drupal\Core\Entity\EntityTypeInterface;
  8. use Symfony\Component\DependencyInjection\ContainerInterface;
  9. /**
  10. * Defines a class to build a listing of action entities.
  11. *
  12. * @see \Drupal\system\Entity\Action
  13. * @see action_entity_info()
  14. */
  15. class ActionListBuilder extends ConfigEntityListBuilder {
  16. /**
  17. * @var bool
  18. */
  19. protected $hasConfigurableActions = FALSE;
  20. /**
  21. * The action plugin manager.
  22. *
  23. * @var \Drupal\Core\Action\ActionManager
  24. */
  25. protected $actionManager;
  26. /**
  27. * Constructs a new ActionListBuilder object.
  28. *
  29. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  30. * The entity type definition.
  31. * @param \Drupal\Core\Entity\EntityStorageInterface $storage
  32. * The action storage.
  33. * @param \Drupal\Core\Action\ActionManager $action_manager
  34. * The action plugin manager.
  35. */
  36. public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, ActionManager $action_manager) {
  37. parent::__construct($entity_type, $storage);
  38. $this->actionManager = $action_manager;
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
  44. return new static(
  45. $entity_type,
  46. $container->get('entity.manager')->getStorage($entity_type->id()),
  47. $container->get('plugin.manager.action')
  48. );
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function load() {
  54. $entities = parent::load();
  55. foreach ($entities as $entity) {
  56. if ($entity->isConfigurable()) {
  57. $this->hasConfigurableActions = TRUE;
  58. continue;
  59. }
  60. }
  61. return $entities;
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function buildRow(EntityInterface $entity) {
  67. $row['type'] = $entity->getType();
  68. $row['label'] = $entity->label();
  69. if ($this->hasConfigurableActions) {
  70. $row += parent::buildRow($entity);
  71. }
  72. return $row;
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public function buildHeader() {
  78. $header = [
  79. 'type' => t('Action type'),
  80. 'label' => t('Label'),
  81. ] + parent::buildHeader();
  82. return $header;
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function getDefaultOperations(EntityInterface $entity) {
  88. $operations = $entity->isConfigurable() ? parent::getDefaultOperations($entity) : [];
  89. if (isset($operations['edit'])) {
  90. $operations['edit']['title'] = t('Configure');
  91. }
  92. return $operations;
  93. }
  94. /**
  95. * {@inheritdoc}
  96. */
  97. public function render() {
  98. $build['action_admin_manage_form'] = \Drupal::formBuilder()->getForm('Drupal\action\Form\ActionAdminManageForm');
  99. $build['action_header']['#markup'] = '<h3>' . $this->t('Available actions:') . '</h3>';
  100. $build['action_table'] = parent::render();
  101. if (!$this->hasConfigurableActions) {
  102. unset($build['action_table']['table']['#header']['operations']);
  103. }
  104. return $build;
  105. }
  106. }