Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
0.00% |
0 / 81 |
|
0.00% |
0 / 9 |
|
0.00% |
0 / 7 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| PreviewPanel | |
0.00% |
0 / 73 |
|
0.00% |
0 / 9 |
|
0.00% |
0 / 7 |
|
0.00% |
0 / 4 |
42 | |
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 | |||
| 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\Island\IslandPluginBase; |
| 12 | use Drupal\display_builder\Island\IslandReloadEventsTrait; |
| 13 | use Drupal\display_builder\Island\IslandType; |
| 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 | default_region: 'main', |
| 27 | icon: 'binoculars', |
| 28 | )] |
| 29 | class PreviewPanel extends IslandPluginBase { |
| 30 | |
| 31 | use IslandReloadEventsTrait; |
| 32 | |
| 33 | /** |
| 34 | * The component element builder. |
| 35 | */ |
| 36 | protected ComponentElementBuilder $componentElementBuilder; |
| 37 | |
| 38 | /** |
| 39 | * {@inheritdoc} |
| 40 | */ |
| 41 | public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): static { |
| 42 | $instance = parent::create($container, $configuration, $plugin_id, $plugin_definition); |
| 43 | $instance->componentElementBuilder = $container->get('ui_patterns.component_element_builder'); |
| 44 | |
| 45 | return $instance; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * {@inheritdoc} |
| 50 | */ |
| 51 | public static function keyboardShortcuts(): array { |
| 52 | return [ |
| 53 | 'key' => 'p', |
| 54 | 'help' => t('Show the preview'), |
| 55 | ]; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * {@inheritdoc} |
| 60 | */ |
| 61 | public function build(InstanceInterface $builder, array $data = [], array $options = []): array { |
| 62 | if (empty($data)) { |
| 63 | return []; |
| 64 | } |
| 65 | |
| 66 | // Replace preview for empty block until #3561447. |
| 67 | $this->alterPreviewPlaceholder($data); |
| 68 | |
| 69 | $returned = []; |
| 70 | |
| 71 | foreach ($data as $slot) { |
| 72 | $build = $this->componentElementBuilder->buildSource([], 'content', [], $slot, $this->configuration['contexts'] ?? []); |
| 73 | $returned[] = $build['#slots']['content'][0] ?? []; |
| 74 | } |
| 75 | |
| 76 | return $returned; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Replace placeholder for preview. |
| 81 | * |
| 82 | * Some block source will not be created, create a simple placeholder to have |
| 83 | * a preview instead of nothing. Until #3561447 is resoled. |
| 84 | * |
| 85 | * @param array $data |
| 86 | * The instance data to replace. |
| 87 | */ |
| 88 | private function alterPreviewPlaceholder(array &$data): void { |
| 89 | $replacements = [ |
| 90 | [ |
| 91 | 'search' => ['plugin_id' => 'system_messages_block'], |
| 92 | 'new_value_title' => new TranslatableMarkup('[Placeholder] Block messages'), |
| 93 | ], |
| 94 | [ |
| 95 | 'search' => ['source_id' => 'page_title'], |
| 96 | 'new_value_title' => new TranslatableMarkup('[Placeholder] Page title'), |
| 97 | ], |
| 98 | [ |
| 99 | 'search' => ['source_id' => 'main_page_content'], |
| 100 | 'new_value_title' => new TranslatableMarkup('[Placeholder] Page content'), |
| 101 | 'new_value_class' => 'db-preview-placeholder-lg', |
| 102 | ], |
| 103 | [ |
| 104 | 'search' => ['source_id' => 'view_attachment_before'], |
| 105 | 'new_value_title' => new TranslatableMarkup('[Placeholder] View attachment before'), |
| 106 | 'new_value_class' => 'db-preview-placeholder-md', |
| 107 | ], |
| 108 | [ |
| 109 | 'search' => ['source_id' => 'view_exposed'], |
| 110 | 'new_value_title' => new TranslatableMarkup('[Placeholder] View exposed form'), |
| 111 | 'new_value_class' => 'db-preview-placeholder-md', |
| 112 | ], |
| 113 | [ |
| 114 | 'search' => ['source_id' => 'view_header'], |
| 115 | 'new_value_title' => new TranslatableMarkup('[Placeholder] View header'), |
| 116 | 'new_value_class' => 'db-preview-placeholder-md', |
| 117 | ], |
| 118 | [ |
| 119 | 'search' => ['source_id' => 'view_rows'], |
| 120 | 'new_value_title' => new TranslatableMarkup('[Placeholder] View rows'), |
| 121 | 'new_value_class' => 'db-preview-placeholder-lg', |
| 122 | ], |
| 123 | [ |
| 124 | 'search' => ['source_id' => 'view_attachment_after'], |
| 125 | 'new_value_title' => new TranslatableMarkup('[Placeholder] View attachment after'), |
| 126 | 'new_value_class' => 'db-preview-placeholder-md', |
| 127 | ], |
| 128 | [ |
| 129 | 'search' => ['source_id' => 'view_pager'], |
| 130 | 'new_value_title' => new TranslatableMarkup('[Placeholder] View pager'), |
| 131 | ], |
| 132 | [ |
| 133 | 'search' => ['source_id' => 'view_more'], |
| 134 | 'new_value_title' => new TranslatableMarkup('[Placeholder] View more'), |
| 135 | ], |
| 136 | [ |
| 137 | 'search' => ['source_id' => 'view_footer'], |
| 138 | 'new_value_title' => new TranslatableMarkup('[Placeholder] View footer'), |
| 139 | 'new_value_class' => 'db-preview-placeholder-md', |
| 140 | ], |
| 141 | [ |
| 142 | 'search' => ['source_id' => 'view_feed_icons'], |
| 143 | 'new_value_title' => new TranslatableMarkup('[Placeholder] View feed icons'), |
| 144 | ], |
| 145 | ]; |
| 146 | |
| 147 | DisplayBuilderHelpers::findAndReplaceInArray($data, $replacements); |
| 148 | } |
| 149 | |
| 150 | } |