Code Coverage
 
Lines
Branches
Paths
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
BackButton
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 3
90
0.00% covered (danger)
0.00%
0 / 1
 build
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
30
 hasButtons
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 findParentDisplayFromId
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3declare(strict_types=1);
4
5namespace Drupal\display_builder\Plugin\display_builder\Island;
6
7use Drupal\Core\StringTranslation\TranslatableMarkup;
8use Drupal\Core\Url;
9use Drupal\display_builder\Attribute\Island;
10use Drupal\display_builder\InstanceInterface;
11use Drupal\display_builder\IslandPluginToolbarButtonConfigurationBase;
12use Drupal\display_builder\IslandType;
13
14/**
15 * Help parent link island plugin implementation.
16 */
17#[Island(
18  id: 'back',
19  enabled_by_default: TRUE,
20  label: new TranslatableMarkup('Back'),
21  description: new TranslatableMarkup('Exit the display builder and go back to admin UI.'),
22  type: IslandType::Button,
23)]
24class BackButton extends IslandPluginToolbarButtonConfigurationBase {
25
26  /**
27   * {@inheritdoc}
28   */
29  public function build(InstanceInterface $builder, array $data = [], array $options = []): array {
30    $url = self::findParentDisplayFromId((string) $builder->id());
31
32    if (!$url || !$this->isButtonEnabled('back')) {
33      return [];
34    }
35
36    $button = $this->buildButton(
37      ($this->showLabel('back')) ? $this->t('Back') : '',
38      'back',
39      $this->showIcon('back') ? 'box-arrow-up-right' : '',
40      $this->t('Exit without losing any data.'),
41    );
42    $button['#attributes']['href'] = $url->toString();
43
44    return $button;
45  }
46
47  /**
48   * {@inheritdoc}
49   */
50  protected function hasButtons(): array {
51    return [
52      'back' => [
53        'title' => $this->t('Back'),
54        'default' => 'icon',
55      ],
56    ];
57  }
58
59  /**
60   * Simply determine the parent display type from id.
61   *
62   * @param string $instance_id
63   *   The builder instance ID.
64   *
65   * @return \Drupal\Core\Url|null
66   *   The url of the instance.
67   */
68  private static function findParentDisplayFromId(string $instance_id): ?Url {
69    // phpcs:ignore-next-line Drupal.DeprecatedFunctions.GlobalFunction
70    $providers = \Drupal::moduleHandler()->invokeAll('display_builder_provider_info');
71
72    foreach ($providers as $provider) {
73      if (\str_starts_with($instance_id, $provider['prefix'])) {
74        return $provider['class']::getDisplayUrlFromInstanceId($instance_id);
75      }
76    }
77
78    return NULL;
79  }
80
81}