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