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