Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 32 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
PreviewPanel | |
0.00% |
0 / 25 |
|
0.00% |
0 / 9 |
182 | |
0.00% |
0 / 1 |
create | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
keyboardShortcuts | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
build | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
30 | |||
onAttachToRoot | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
onAttachToSlot | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
onMove | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
onHistoryChange | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
onUpdate | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
onDelete | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
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\display_builder\Attribute\Island; |
9 | use Drupal\display_builder\DisplayBuilderHelpers; |
10 | use Drupal\display_builder\InstanceInterface; |
11 | use Drupal\display_builder\IslandPluginBase; |
12 | use Drupal\display_builder\IslandType; |
13 | use Drupal\display_builder_page_layout\Entity\PageLayout; |
14 | use Drupal\ui_patterns\Element\ComponentElementBuilder; |
15 | use Symfony\Component\DependencyInjection\ContainerInterface; |
16 | |
17 | /** |
18 | * Preview island plugin implementation. |
19 | */ |
20 | #[Island( |
21 | id: 'preview', |
22 | enabled_by_default: TRUE, |
23 | label: new TranslatableMarkup('Preview'), |
24 | description: new TranslatableMarkup('Show a real time preview of the display.'), |
25 | type: IslandType::View, |
26 | icon: 'binoculars', |
27 | )] |
28 | class PreviewPanel extends IslandPluginBase { |
29 | |
30 | /** |
31 | * The component element builder. |
32 | */ |
33 | protected ComponentElementBuilder $componentElementBuilder; |
34 | |
35 | /** |
36 | * {@inheritdoc} |
37 | */ |
38 | public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): static { |
39 | $instance = parent::create($container, $configuration, $plugin_id, $plugin_definition); |
40 | $instance->componentElementBuilder = $container->get('ui_patterns.component_element_builder'); |
41 | |
42 | return $instance; |
43 | } |
44 | |
45 | /** |
46 | * {@inheritdoc} |
47 | */ |
48 | public static function keyboardShortcuts(): array { |
49 | return [ |
50 | 'p' => t('Show the preview'), |
51 | ]; |
52 | } |
53 | |
54 | /** |
55 | * {@inheritdoc} |
56 | */ |
57 | public function build(InstanceInterface $builder, array $data = [], array $options = []): array { |
58 | $builder_id = (string) $builder->id(); |
59 | |
60 | if (empty($data)) { |
61 | return []; |
62 | } |
63 | |
64 | // Page layout display need preview. We don't have source for title and |
65 | // content, so let replace it on the fly for preview. |
66 | if (\class_exists('PageLayout') && \str_starts_with($builder_id, PageLayout::getPrefix())) { |
67 | $content_placeholder = '<div class="db-background db-preview-placeholder"><h2>[Page] Content placeholder</h2></div>'; |
68 | $title_placeholder = '<div class="db-background db-preview-placeholder"><h1 class="title">[Page] Title placeholder</h1></div>'; |
69 | |
70 | // @todo one pass and placeholder style? |
71 | DisplayBuilderHelpers::findArrayReplaceSource($data, ['source_id' => 'page_title'], ['#markup' => $title_placeholder]); |
72 | DisplayBuilderHelpers::findArrayReplaceSource($data, ['source_id' => 'main_page_content'], ['#markup' => $content_placeholder]); |
73 | } |
74 | |
75 | $returned = []; |
76 | |
77 | foreach ($data as $slot) { |
78 | $build = $this->componentElementBuilder->buildSource([], 'content', [], $slot, $this->configuration['contexts'] ?? []); |
79 | $returned[] = $build['#slots']['content'][0] ?? []; |
80 | } |
81 | |
82 | return $returned; |
83 | } |
84 | |
85 | /** |
86 | * {@inheritdoc} |
87 | */ |
88 | public function onAttachToRoot(string $builder_id, string $instance_id): array { |
89 | return $this->reloadWithGlobalData($builder_id); |
90 | } |
91 | |
92 | /** |
93 | * {@inheritdoc} |
94 | */ |
95 | public function onAttachToSlot(string $builder_id, string $instance_id, string $parent_id): array { |
96 | return $this->reloadWithGlobalData($builder_id); |
97 | } |
98 | |
99 | /** |
100 | * {@inheritdoc} |
101 | */ |
102 | public function onMove(string $builder_id, string $instance_id): array { |
103 | return $this->reloadWithGlobalData($builder_id); |
104 | } |
105 | |
106 | /** |
107 | * {@inheritdoc} |
108 | */ |
109 | public function onHistoryChange(string $builder_id): array { |
110 | return $this->reloadWithGlobalData($builder_id); |
111 | } |
112 | |
113 | /** |
114 | * {@inheritdoc} |
115 | */ |
116 | public function onUpdate(string $builder_id, string $instance_id): array { |
117 | return $this->reloadWithGlobalData($builder_id); |
118 | } |
119 | |
120 | /** |
121 | * {@inheritdoc} |
122 | */ |
123 | public function onDelete(string $builder_id, string $parent_id): array { |
124 | return $this->reloadWithGlobalData($builder_id); |
125 | } |
126 | |
127 | } |