Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
0.00% |
0 / 31 |
|
0.00% |
0 / 19 |
|
0.00% |
0 / 16 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| BackButton | |
0.00% |
0 / 24 |
|
0.00% |
0 / 19 |
|
0.00% |
0 / 16 |
|
0.00% |
0 / 4 |
110 | |
0.00% |
0 / 1 |
| create | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| build | |
0.00% |
0 / 11 |
|
0.00% |
0 / 11 |
|
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
30 | |||
| hasButtons | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| findParentDisplayFromId | |
0.00% |
0 / 4 |
|
0.00% |
0 / 6 |
|
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| 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\Core\Url; |
| 9 | use Drupal\display_builder\Attribute\Island; |
| 10 | use Drupal\display_builder\DisplayBuildablePluginManager; |
| 11 | use Drupal\display_builder\InstanceInterface; |
| 12 | use Drupal\display_builder\Island\IslandPluginToolbarButtonConfigurationBase; |
| 13 | use Drupal\display_builder\Island\IslandType; |
| 14 | use Symfony\Component\DependencyInjection\ContainerInterface; |
| 15 | |
| 16 | /** |
| 17 | * Help parent link island plugin implementation. |
| 18 | */ |
| 19 | #[Island( |
| 20 | id: 'back', |
| 21 | enabled_by_default: TRUE, |
| 22 | label: new TranslatableMarkup('Back'), |
| 23 | description: new TranslatableMarkup('Exit the display builder and go back to admin UI.'), |
| 24 | type: IslandType::Button, |
| 25 | default_region: 'end', |
| 26 | )] |
| 27 | class BackButton extends IslandPluginToolbarButtonConfigurationBase { |
| 28 | |
| 29 | /** |
| 30 | * The display buildable plugin manager. |
| 31 | */ |
| 32 | protected DisplayBuildablePluginManager $displayBuildableManager; |
| 33 | |
| 34 | /** |
| 35 | * {@inheritdoc} |
| 36 | */ |
| 37 | public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): static { |
| 38 | $instance = parent::create($container, $configuration, $plugin_id, $plugin_definition); |
| 39 | $instance->displayBuildableManager = $container->get('plugin.manager.display_buildable'); |
| 40 | |
| 41 | return $instance; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * {@inheritdoc} |
| 46 | */ |
| 47 | public function build(InstanceInterface $builder, array $data = [], array $options = []): array { |
| 48 | $url = $this->findParentDisplayFromId((string) $builder->id()); |
| 49 | |
| 50 | if (!$url || !$this->isButtonEnabled('back')) { |
| 51 | return []; |
| 52 | } |
| 53 | |
| 54 | $button = $this->buildButton( |
| 55 | ($this->showLabel('back')) ? $this->t('Back') : '', |
| 56 | 'back', |
| 57 | $this->showIcon('back') ? 'box-arrow-up-right' : '', |
| 58 | $this->t('Exit without losing any data.'), |
| 59 | ); |
| 60 | $button['#attributes']['href'] = $url->toString(); |
| 61 | |
| 62 | return $button; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * {@inheritdoc} |
| 67 | */ |
| 68 | protected function hasButtons(): array { |
| 69 | return [ |
| 70 | 'back' => [ |
| 71 | 'title' => $this->t('Back'), |
| 72 | 'default' => 'icon', |
| 73 | ], |
| 74 | ]; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Determine the parent display URL from the instance ID. |
| 79 | * |
| 80 | * @param string $instance_id |
| 81 | * The builder instance ID. |
| 82 | * |
| 83 | * @return \Drupal\Core\Url|null |
| 84 | * The URL of the parent display, or NULL if not found. |
| 85 | */ |
| 86 | private function findParentDisplayFromId(string $instance_id): ?Url { |
| 87 | foreach ($this->displayBuildableManager->getDefinitions() as $provider) { |
| 88 | if (\str_starts_with($instance_id, $provider['instance_prefix'])) { |
| 89 | return $provider['class']::getDisplayUrlFromInstanceId($instance_id); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | return NULL; |
| 94 | } |
| 95 | |
| 96 | } |