Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 34 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
PresetLibraryPanel | |
0.00% |
0 / 29 |
|
0.00% |
0 / 5 |
56 | |
0.00% |
0 / 1 |
create | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
label | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
build | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
6 | |||
onPresetSave | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
buildPresets | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace Drupal\display_builder\Plugin\display_builder\Island; |
6 | |
7 | use Drupal\Core\Entity\EntityStorageInterface; |
8 | use Drupal\Core\StringTranslation\TranslatableMarkup; |
9 | use Drupal\Core\Url; |
10 | use Drupal\display_builder\Attribute\Island; |
11 | use Drupal\display_builder\InstanceInterface; |
12 | use Drupal\display_builder\IslandPluginBase; |
13 | use Drupal\display_builder\IslandType; |
14 | use Symfony\Component\DependencyInjection\ContainerInterface; |
15 | |
16 | /** |
17 | * Preset island plugin implementation. |
18 | */ |
19 | #[Island( |
20 | id: 'preset_library', |
21 | label: new TranslatableMarkup('Preset library'), |
22 | description: new TranslatableMarkup('List of preset, already build group of components.'), |
23 | type: IslandType::Library, |
24 | )] |
25 | class PresetLibraryPanel extends IslandPluginBase { |
26 | |
27 | /** |
28 | * The Pattern preset storage. |
29 | */ |
30 | protected EntityStorageInterface $presetConfigStorage; |
31 | |
32 | /** |
33 | * {@inheritdoc} |
34 | */ |
35 | public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): static { |
36 | $instance = parent::create($container, $configuration, $plugin_id, $plugin_definition); |
37 | $instance->presetConfigStorage = $container->get('entity_type.manager')->getStorage('pattern_preset'); |
38 | |
39 | return $instance; |
40 | } |
41 | |
42 | /** |
43 | * {@inheritdoc} |
44 | */ |
45 | public function label(): string { |
46 | return 'Presets'; |
47 | } |
48 | |
49 | /** |
50 | * {@inheritdoc} |
51 | */ |
52 | public function build(InstanceInterface $builder, array $data = [], array $options = []): array { |
53 | $builder_id = (string) $builder->id(); |
54 | /** @var \Drupal\display_builder\PatternPresetInterface[] $presets */ |
55 | $presets = $this->presetConfigStorage->loadByProperties(['status' => TRUE]); |
56 | |
57 | if (empty($presets)) { |
58 | $content = [ |
59 | '#type' => 'html_tag', |
60 | '#tag' => 'em', |
61 | '#value' => $this->t('No preset yet.'), |
62 | ]; |
63 | } |
64 | else { |
65 | $content = $this->buildPresets($builder_id, $presets); |
66 | } |
67 | |
68 | return [ |
69 | '#type' => 'component', |
70 | '#component' => 'display_builder:library_panel', |
71 | '#slots' => [ |
72 | 'content' => $content, |
73 | ], |
74 | ]; |
75 | } |
76 | |
77 | /** |
78 | * {@inheritdoc} |
79 | */ |
80 | public function onPresetSave(string $builder_id): array { |
81 | return $this->reloadWithGlobalData($builder_id); |
82 | } |
83 | |
84 | /** |
85 | * Build preset as placeholder. |
86 | * |
87 | * @param string $builder_id |
88 | * Builder ID. |
89 | * @param \Drupal\display_builder\PatternPresetInterface[] $presets |
90 | * The presets to build. |
91 | * |
92 | * @return array |
93 | * Array of preset plugins. |
94 | */ |
95 | protected function buildPresets(string $builder_id, array $presets): array { |
96 | $build = []; |
97 | |
98 | foreach ($presets as $preset_id => $preset) { |
99 | $keywords = \sprintf('%s %s', $preset->get('label'), $preset->get('description') ?? ''); |
100 | $preset_preview_url = Url::fromRoute('display_builder.api_preset_preview', ['preset_id' => $preset_id]); |
101 | $build[] = $this->buildPlaceholderButtonWithPreview($builder_id, $preset->get('label'), ['preset_id' => $preset_id], $preset_preview_url, $keywords); |
102 | } |
103 | $build = $this->buildDraggables($builder_id, $build); |
104 | $build['#source_contexts'] = $this->configuration['contexts'] ?? []; |
105 | |
106 | return $build; |
107 | } |
108 | |
109 | } |