Code Coverage
 
Lines
Branches
Paths
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 15
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 / 18
0.00% covered (danger)
0.00%
0 / 15
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 / 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 / 5
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\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  default_region: 'end',
24)]
25class BackButton extends IslandPluginToolbarButtonConfigurationBase {
26
27  /**
28   * {@inheritdoc}
29   */
30  public function build(InstanceInterface $builder, array $data = [], array $options = []): array {
31    $url = self::findParentDisplayFromId((string) $builder->id());
32
33    if (!$url || !$this->isButtonEnabled('back')) {
34      return [];
35    }
36
37    $button = $this->buildButton(
38      ($this->showLabel('back')) ? $this->t('Back') : '',
39      'back',
40      $this->showIcon('back') ? 'box-arrow-up-right' : '',
41      $this->t('Exit without losing any data.'),
42    );
43    $button['#attributes']['href'] = $url->toString();
44
45    return $button;
46  }
47
48  /**
49   * {@inheritdoc}
50   */
51  protected function hasButtons(): array {
52    return [
53      'back' => [
54        'title' => $this->t('Back'),
55        'default' => 'icon',
56      ],
57    ];
58  }
59
60  /**
61   * Simply determine the parent display type from id.
62   *
63   * @param string $instance_id
64   *   The builder instance ID.
65   *
66   * @return \Drupal\Core\Url|null
67   *   The url of the instance.
68   */
69  private static function findParentDisplayFromId(string $instance_id): ?Url {
70    $providers = \Drupal::service('plugin.manager.display_buildable')->getDefinitions();
71
72    foreach ($providers as $provider) {
73      if (\str_starts_with($instance_id, $provider['instance_prefix'])) {
74        return $provider['class']::getDisplayUrlFromInstanceId($instance_id);
75      }
76    }
77
78    return NULL;
79  }
80
81}

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
30  public function build(InstanceInterface $builder, array $data = [], array $options = []): array {
31    $url = self::findParentDisplayFromId((string) $builder->id());
32
33    if (!$url || !$this->isButtonEnabled('back')) {
33    if (!$url || !$this->isButtonEnabled('back')) {
33    if (!$url || !$this->isButtonEnabled('back')) {
34      return [];
37    $button = $this->buildButton(
38      ($this->showLabel('back')) ? $this->t('Back') : '',
38      ($this->showLabel('back')) ? $this->t('Back') : '',
38      ($this->showLabel('back')) ? $this->t('Back') : '',
38      ($this->showLabel('back')) ? $this->t('Back') : '',
39      'back',
40      $this->showIcon('back') ? 'box-arrow-up-right' : '',
40      $this->showIcon('back') ? 'box-arrow-up-right' : '',
40      $this->showIcon('back') ? 'box-arrow-up-right' : '',
40      $this->showIcon('back') ? 'box-arrow-up-right' : '',
41      $this->t('Exit without losing any data.'),
42    );
43    $button['#attributes']['href'] = $url->toString();
44
45    return $button;
46  }
BackButton->findParentDisplayFromId
69  private static function findParentDisplayFromId(string $instance_id): ?Url {
70    $providers = \Drupal::service('plugin.manager.display_buildable')->getDefinitions();
71
72    foreach ($providers as $provider) {
72    foreach ($providers as $provider) {
73      if (\str_starts_with($instance_id, $provider['instance_prefix'])) {
74        return $provider['class']::getDisplayUrlFromInstanceId($instance_id);
72    foreach ($providers as $provider) {
72    foreach ($providers as $provider) {
73      if (\str_starts_with($instance_id, $provider['instance_prefix'])) {
74        return $provider['class']::getDisplayUrlFromInstanceId($instance_id);
75      }
76    }
77
78    return NULL;
79  }
BackButton->hasButtons
54        'title' => $this->t('Back'),
55        'default' => 'icon',
56      ],
57    ];
58  }