Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
0.00% |
0 / 54 |
|
0.00% |
0 / 15 |
|
0.00% |
0 / 14 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| PresetLibraryPanel | |
0.00% |
0 / 49 |
|
0.00% |
0 / 15 |
|
0.00% |
0 / 14 |
|
0.00% |
0 / 5 |
90 | |
0.00% |
0 / 1 |
| create | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| label | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| build | |
0.00% |
0 / 36 |
|
0.00% |
0 / 8 |
|
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
| onPresetSave | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| buildPresets | |
0.00% |
0 / 8 |
|
0.00% |
0 / 4 |
|
0.00% |
0 / 3 |
|
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 | $entity_ids = $this->presetConfigStorage->getQuery() |
| 55 | ->accessCheck(TRUE) |
| 56 | ->condition('status', TRUE) |
| 57 | ->sort('weight', 'ASC') |
| 58 | ->execute(); |
| 59 | /** @var \Drupal\display_builder\PatternPresetInterface[] $presets */ |
| 60 | $presets = $this->presetConfigStorage->loadMultiple($entity_ids); |
| 61 | $contexts = $this->configuration['contexts'] ?? []; |
| 62 | |
| 63 | foreach ($presets as $preset_id => $preset) { |
| 64 | if (!$preset->areContextsSatisfied($contexts)) { |
| 65 | unset($presets[$preset_id]); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | if (empty($presets)) { |
| 70 | $content = [ |
| 71 | [ |
| 72 | '#type' => 'html_tag', |
| 73 | '#tag' => 'p', |
| 74 | ], |
| 75 | [ |
| 76 | '#type' => 'html_tag', |
| 77 | '#tag' => 'p', |
| 78 | '#value' => $this->t('Pattern presets are reusable arrangements of components and blocks.'), |
| 79 | ], |
| 80 | [ |
| 81 | '#type' => 'html_tag', |
| 82 | '#tag' => 'p', |
| 83 | '#value' => $this->t('Add presets from the contextual menu.'), |
| 84 | ], |
| 85 | ]; |
| 86 | } |
| 87 | else { |
| 88 | $content = $this->buildPresets($builder_id, $presets); |
| 89 | } |
| 90 | |
| 91 | return [ |
| 92 | '#type' => 'component', |
| 93 | '#component' => 'display_builder:library_panel', |
| 94 | '#slots' => [ |
| 95 | 'content' => $content, |
| 96 | ], |
| 97 | ]; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * {@inheritdoc} |
| 102 | */ |
| 103 | public function onPresetSave(string $builder_id): array { |
| 104 | return $this->reloadWithGlobalData($builder_id); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Build preset as placeholder. |
| 109 | * |
| 110 | * @param string $builder_id |
| 111 | * Builder ID. |
| 112 | * @param \Drupal\display_builder\PatternPresetInterface[] $presets |
| 113 | * The presets to build. |
| 114 | * |
| 115 | * @return array |
| 116 | * Array of preset plugins. |
| 117 | */ |
| 118 | protected function buildPresets(string $builder_id, array $presets): array { |
| 119 | $build = []; |
| 120 | |
| 121 | foreach ($presets as $preset_id => $preset) { |
| 122 | $keywords = \sprintf('%s %s', $preset->get('label'), $preset->get('description') ?? ''); |
| 123 | $preset_preview_url = Url::fromRoute('display_builder.api_preset_preview', ['preset_id' => $preset_id]); |
| 124 | $build[] = $this->buildPlaceholderButtonWithPreview($builder_id, $preset->get('label'), ['preset_id' => $preset_id], $preset_preview_url, $keywords); |
| 125 | } |
| 126 | $build = $this->buildDraggables($builder_id, $build); |
| 127 | $build['#source_contexts'] = $this->configuration['contexts'] ?? []; |
| 128 | |
| 129 | return $build; |
| 130 | } |
| 131 | |
| 132 | } |