Code Coverage
 
Lines
Branches
Paths
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
BackButton
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 4
110
0.00% covered (danger)
0.00%
0 / 1
 create
0.00% covered (danger)
0.00%
0 / 3
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
 build
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 10
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 / 4
0.00% covered (danger)
0.00%
0 / 6
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\DisplayBuildablePluginManager;
11use Drupal\display_builder\InstanceInterface;
12use Drupal\display_builder\Island\IslandPluginToolbarButtonConfigurationBase;
13use Drupal\display_builder\Island\IslandType;
14use 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)]
27class 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}

Branches

Below are the source code lines that represent each code branch as identified by Xdebug. Please note a branch is not necessarily coterminous with a line, a line may contain multiple branches and therefore show up more than once. Please also be aware that some branches may be implicit rather than explicit, e.g. an if statement always has an else as part of its logical flow even if you didn't write one.

BackButton->build
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')) {
50    if (!$url || !$this->isButtonEnabled('back')) {
50    if (!$url || !$this->isButtonEnabled('back')) {
51      return [];
54    $button = $this->buildButton(
55      ($this->showLabel('back')) ? $this->t('Back') : '',
55      ($this->showLabel('back')) ? $this->t('Back') : '',
55      ($this->showLabel('back')) ? $this->t('Back') : '',
55      ($this->showLabel('back')) ? $this->t('Back') : '',
56      'back',
57      $this->showIcon('back') ? 'box-arrow-up-right' : '',
57      $this->showIcon('back') ? 'box-arrow-up-right' : '',
57      $this->showIcon('back') ? 'box-arrow-up-right' : '',
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  }
BackButton->create
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  }
BackButton->findParentDisplayFromId
86  private function findParentDisplayFromId(string $instance_id): ?Url {
87    foreach ($this->displayBuildableManager->getDefinitions() as $provider) {
87    foreach ($this->displayBuildableManager->getDefinitions() as $provider) {
88      if (\str_starts_with($instance_id, $provider['instance_prefix'])) {
89        return $provider['class']::getDisplayUrlFromInstanceId($instance_id);
87    foreach ($this->displayBuildableManager->getDefinitions() as $provider) {
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  }
BackButton->hasButtons
71        'title' => $this->t('Back'),
72        'default' => 'icon',
73      ],
74    ];
75  }