Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
0.00% |
0 / 87 |
|
0.00% |
0 / 15 |
|
0.00% |
0 / 13 |
|
0.00% |
0 / 10 |
CRAP | |
0.00% |
0 / 1 |
| PreviewPanel | |
0.00% |
0 / 79 |
|
0.00% |
0 / 15 |
|
0.00% |
0 / 13 |
|
0.00% |
0 / 10 |
156 | |
0.00% |
0 / 1 |
| create | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| keyboardShortcuts | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| build | |
0.00% |
0 / 8 |
|
0.00% |
0 / 6 |
|
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| onAttachToRoot | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| onAttachToSlot | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| onMove | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| onHistoryChange | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| onUpdate | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| onDelete | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| alterPreviewPlaceholder | |
0.00% |
0 / 58 |
|
0.00% |
0 / 1 |
|
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\ui_patterns\Element\ComponentElementBuilder; |
| 14 | use Symfony\Component\DependencyInjection\ContainerInterface; |
| 15 | |
| 16 | /** |
| 17 | * Preview island plugin implementation. |
| 18 | */ |
| 19 | #[Island( |
| 20 | id: 'preview', |
| 21 | enabled_by_default: TRUE, |
| 22 | label: new TranslatableMarkup('Preview'), |
| 23 | description: new TranslatableMarkup('Show a real time preview of the display.'), |
| 24 | type: IslandType::View, |
| 25 | default_region: 'main', |
| 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 | 'key' => 'p', |
| 51 | 'help' => t('Show the preview'), |
| 52 | ]; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * {@inheritdoc} |
| 57 | */ |
| 58 | public function build(InstanceInterface $builder, array $data = [], array $options = []): array { |
| 59 | if (empty($data)) { |
| 60 | return []; |
| 61 | } |
| 62 | |
| 63 | // Replace preview for empty block until #3561447. |
| 64 | $this->alterPreviewPlaceholder($data); |
| 65 | |
| 66 | $returned = []; |
| 67 | |
| 68 | foreach ($data as $slot) { |
| 69 | $build = $this->componentElementBuilder->buildSource([], 'content', [], $slot, $this->configuration['contexts'] ?? []); |
| 70 | $returned[] = $build['#slots']['content'][0] ?? []; |
| 71 | } |
| 72 | |
| 73 | return $returned; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * {@inheritdoc} |
| 78 | */ |
| 79 | public function onAttachToRoot(string $builder_id, string $instance_id): array { |
| 80 | return $this->reloadWithGlobalData($builder_id); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * {@inheritdoc} |
| 85 | */ |
| 86 | public function onAttachToSlot(string $builder_id, string $instance_id, string $parent_id): array { |
| 87 | return $this->reloadWithGlobalData($builder_id); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * {@inheritdoc} |
| 92 | */ |
| 93 | public function onMove(string $builder_id, string $instance_id): array { |
| 94 | return $this->reloadWithGlobalData($builder_id); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * {@inheritdoc} |
| 99 | */ |
| 100 | public function onHistoryChange(string $builder_id): array { |
| 101 | return $this->reloadWithGlobalData($builder_id); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * {@inheritdoc} |
| 106 | */ |
| 107 | public function onUpdate(string $builder_id, string $instance_id): array { |
| 108 | return $this->reloadWithGlobalData($builder_id); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * {@inheritdoc} |
| 113 | */ |
| 114 | public function onDelete(string $builder_id, string $parent_id): array { |
| 115 | return $this->reloadWithGlobalData($builder_id); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Replace placeholder for preview. |
| 120 | * |
| 121 | * Some block source will not be created, create a simple placeholder to have |
| 122 | * a preview instead of nothing. Until #3561447 is resoled. |
| 123 | * |
| 124 | * @param array $data |
| 125 | * The instance data to replace. |
| 126 | */ |
| 127 | private function alterPreviewPlaceholder(array &$data): void { |
| 128 | $replacements = [ |
| 129 | [ |
| 130 | 'search' => ['plugin_id' => 'system_messages_block'], |
| 131 | 'new_value_title' => new TranslatableMarkup('[Placeholder] Block messages'), |
| 132 | ], |
| 133 | [ |
| 134 | 'search' => ['source_id' => 'page_title'], |
| 135 | 'new_value_title' => new TranslatableMarkup('[Placeholder] Page title'), |
| 136 | ], |
| 137 | [ |
| 138 | 'search' => ['source_id' => 'main_page_content'], |
| 139 | 'new_value_title' => new TranslatableMarkup('[Placeholder] Page content'), |
| 140 | 'new_value_class' => 'db-preview-placeholder-lg', |
| 141 | ], |
| 142 | [ |
| 143 | 'search' => ['source_id' => 'view_attachment_before'], |
| 144 | 'new_value_title' => new TranslatableMarkup('[Placeholder] View attachment before'), |
| 145 | 'new_value_class' => 'db-preview-placeholder-md', |
| 146 | ], |
| 147 | [ |
| 148 | 'search' => ['source_id' => 'view_exposed'], |
| 149 | 'new_value_title' => new TranslatableMarkup('[Placeholder] View exposed form'), |
| 150 | 'new_value_class' => 'db-preview-placeholder-md', |
| 151 | ], |
| 152 | [ |
| 153 | 'search' => ['source_id' => 'view_header'], |
| 154 | 'new_value_title' => new TranslatableMarkup('[Placeholder] View header'), |
| 155 | 'new_value_class' => 'db-preview-placeholder-md', |
| 156 | ], |
| 157 | [ |
| 158 | 'search' => ['source_id' => 'view_rows'], |
| 159 | 'new_value_title' => new TranslatableMarkup('[Placeholder] View rows'), |
| 160 | 'new_value_class' => 'db-preview-placeholder-lg', |
| 161 | ], |
| 162 | [ |
| 163 | 'search' => ['source_id' => 'view_attachment_after'], |
| 164 | 'new_value_title' => new TranslatableMarkup('[Placeholder] View attachment after'), |
| 165 | 'new_value_class' => 'db-preview-placeholder-md', |
| 166 | ], |
| 167 | [ |
| 168 | 'search' => ['source_id' => 'view_pager'], |
| 169 | 'new_value_title' => new TranslatableMarkup('[Placeholder] View pager'), |
| 170 | ], |
| 171 | [ |
| 172 | 'search' => ['source_id' => 'view_more'], |
| 173 | 'new_value_title' => new TranslatableMarkup('[Placeholder] View more'), |
| 174 | ], |
| 175 | [ |
| 176 | 'search' => ['source_id' => 'view_footer'], |
| 177 | 'new_value_title' => new TranslatableMarkup('[Placeholder] View footer'), |
| 178 | 'new_value_class' => 'db-preview-placeholder-md', |
| 179 | ], |
| 180 | [ |
| 181 | 'search' => ['source_id' => 'view_feed_icons'], |
| 182 | 'new_value_title' => new TranslatableMarkup('[Placeholder] View feed icons'), |
| 183 | ], |
| 184 | ]; |
| 185 | |
| 186 | DisplayBuilderHelpers::findAndReplaceInArray($data, $replacements); |
| 187 | } |
| 188 | |
| 189 | } |