Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 26 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
ParentDisplayButton | |
0.00% |
0 / 20 |
|
0.00% |
0 / 2 |
30 | |
0.00% |
0 / 1 |
build | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
6 | |||
findParentDisplayFromId | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace Drupal\display_builder\Plugin\display_builder\Island; |
6 | |
7 | use Drupal\Core\StringTranslation\TranslatableMarkup; |
8 | use Drupal\Core\Url; |
9 | use Drupal\display_builder\Attribute\Island; |
10 | use Drupal\display_builder\InstanceInterface; |
11 | use Drupal\display_builder\IslandPluginBase; |
12 | use Drupal\display_builder\IslandType; |
13 | |
14 | /** |
15 | * Help parent link island plugin implementation. |
16 | */ |
17 | #[Island( |
18 | id: 'parent_display', |
19 | enabled_by_default: TRUE, |
20 | label: new TranslatableMarkup('Parent display link'), |
21 | description: new TranslatableMarkup('Allow a direct link to parent display.'), |
22 | type: IslandType::Button, |
23 | )] |
24 | class ParentDisplayButton extends IslandPluginBase { |
25 | |
26 | /** |
27 | * {@inheritdoc} |
28 | */ |
29 | public function build(InstanceInterface $builder, array $data = [], array $options = []): array { |
30 | $url = self::findParentDisplayFromId((string) $builder->id()); |
31 | |
32 | if (!$url) { |
33 | return []; |
34 | } |
35 | |
36 | $button = [ |
37 | '#type' => 'component', |
38 | '#component' => 'display_builder:button', |
39 | '#props' => [ |
40 | 'icon' => 'box-arrow-up-right', |
41 | 'tooltip' => $this->t('Go to the parent display that manage this instance.'), |
42 | ], |
43 | '#attributes' => [ |
44 | 'href' => $url->toString(), |
45 | ], |
46 | ]; |
47 | |
48 | return $button; |
49 | } |
50 | |
51 | /** |
52 | * Simply determine the parent display type from id. |
53 | * |
54 | * @param string $instance_id |
55 | * The builder instance ID. |
56 | * |
57 | * @return \Drupal\Core\Url|null |
58 | * The url of the instance. |
59 | */ |
60 | private static function findParentDisplayFromId(string $instance_id): ?Url { |
61 | // phpcs:ignore-next-line Drupal.DeprecatedFunctions.GlobalFunction |
62 | $providers = \Drupal::moduleHandler()->invokeAll('display_builder_provider_info'); |
63 | |
64 | foreach ($providers as $provider) { |
65 | if (\str_starts_with($instance_id, $provider['prefix'])) { |
66 | return $provider['class']::getDisplayUrlFromInstanceId($instance_id); |
67 | } |
68 | } |
69 | |
70 | return NULL; |
71 | } |
72 | |
73 | } |