Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 35 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
InstanceListBuilder | |
0.00% |
0 / 35 |
|
0.00% |
0 / 7 |
110 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
createInstance | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
getFormId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
buildHeader | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
render | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
buildRow | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
20 | |||
getEntityIds | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace Drupal\display_builder_ui; |
6 | |
7 | use Drupal\Core\Datetime\DateFormatterInterface; |
8 | use Drupal\Core\Entity\EntityInterface; |
9 | use Drupal\Core\Entity\EntityListBuilder; |
10 | use Drupal\Core\Entity\EntityStorageInterface; |
11 | use Drupal\Core\Entity\EntityTypeInterface; |
12 | use Drupal\display_builder\DisplayBuilderHelpers; |
13 | use Symfony\Component\DependencyInjection\ContainerInterface; |
14 | |
15 | /** |
16 | * Provides a listing of display builders instances. |
17 | */ |
18 | final class InstanceListBuilder extends EntityListBuilder { |
19 | |
20 | /** |
21 | * {@inheritdoc} |
22 | */ |
23 | public function __construct( |
24 | protected EntityTypeInterface $entity_type, |
25 | EntityStorageInterface $storage, |
26 | private readonly DateFormatterInterface $dateFormatter, |
27 | ) { |
28 | parent::__construct($entity_type, $storage); |
29 | } |
30 | |
31 | /** |
32 | * {@inheritdoc} |
33 | */ |
34 | public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type): self { |
35 | return new self( |
36 | $entity_type, |
37 | $container->get('entity_type.manager')->getStorage($entity_type->id()), |
38 | $container->get('date.formatter'), |
39 | ); |
40 | } |
41 | |
42 | /** |
43 | * {@inheritdoc} |
44 | */ |
45 | public function getFormId(): string { |
46 | return 'display_builder_instance_list_builder'; |
47 | } |
48 | |
49 | /** |
50 | * {@inheritdoc} |
51 | */ |
52 | public function buildHeader(): array { |
53 | $header = [ |
54 | 'id' => $this->t('Instance'), |
55 | 'context' => $this->t('Context'), |
56 | 'updated' => $this->t('Updated'), |
57 | 'log' => $this->t('Last log'), |
58 | ]; |
59 | |
60 | return $header + parent::buildHeader(); |
61 | } |
62 | |
63 | /** |
64 | * {@inheritdoc} |
65 | */ |
66 | public function render(): array { |
67 | $build = parent::render(); |
68 | $build['notice'] = [ |
69 | '#markup' => $this->t('List of all Display builder instances.<br>An instance is a saved arrangement of components and styles for a specific display context (a view mode, a page layout or a view).<br>Instances are created directly from display pages like Entity view, Page layout or Views and should be managed directly from each display context.'), |
70 | '#weight' => -100, |
71 | ]; |
72 | |
73 | return $build; |
74 | } |
75 | |
76 | /** |
77 | * {@inheritdoc} |
78 | */ |
79 | public function buildRow(EntityInterface $instance): array { |
80 | /** @var \Drupal\display_builder\InstanceInterface $instance */ |
81 | $instance_id = (string) $instance->id(); |
82 | $row = []; |
83 | |
84 | $type = '-'; |
85 | $providers = $this->moduleHandler->invokeAll('display_builder_provider_info'); |
86 | |
87 | foreach ($providers as $provider) { |
88 | if (\str_starts_with($instance_id, $provider['prefix'])) { |
89 | $type = $provider['label']; |
90 | |
91 | break; |
92 | } |
93 | } |
94 | |
95 | $row['id']['data'] = $instance_id; |
96 | $row['context']['data'] = $type; |
97 | |
98 | /** @var \Drupal\display_builder\HistoryStep $present */ |
99 | $present = $instance->getCurrent(); |
100 | $row['updated']['data'] = $present->time ? DisplayBuilderHelpers::formatTime($this->dateFormatter, (int) $present->time) : '-'; |
101 | $row['log']['data'] = $present->log ?? '-'; |
102 | |
103 | return $row + parent::buildRow($instance); |
104 | } |
105 | |
106 | /** |
107 | * {@inheritdoc} |
108 | */ |
109 | protected function getEntityIds(): array { |
110 | // To avoid implementing EntityStorageInterface::getQuery() |
111 | return \array_keys($this->getStorage()->loadMultiple()); |
112 | } |
113 | |
114 | } |