Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ParentDisplayButton
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 2
30
0.00% covered (danger)
0.00%
0 / 1
 build
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
6
 findParentDisplayFromId
0.00% covered (danger)
0.00%
0 / 5
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\IslandPluginBase;
12use Drupal\display_builder\IslandType;
13
14/**
15 * Help parent link island plugin implementation.
16 */
17#[Island(
18  id: 'parent_display',
19  enabled_by_default: TRUE,
20  label: new TranslatableMarkup('Parent display link'),
21  description: new TranslatableMarkup('Allow a direct link to parent display.'),
22  type: IslandType::Button,
23)]
24class ParentDisplayButton extends IslandPluginBase {
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) {
33      return [];
34    }
35
36    $button = [
37      '#type' => 'component',
38      '#component' => 'display_builder:button',
39      '#props' => [
40        'icon' => 'box-arrow-up-right',
41        'tooltip' => $this->t('Go to the parent display that manage this instance.'),
42      ],
43      '#attributes' => [
44        'href' => $url->toString(),
45      ],
46    ];
47
48    return $button;
49  }
50
51  /**
52   * Simply determine the parent display type from id.
53   *
54   * @param string $instance_id
55   *   The builder instance ID.
56   *
57   * @return \Drupal\Core\Url|null
58   *   The url of the instance.
59   */
60  private static function findParentDisplayFromId(string $instance_id): ?Url {
61    // phpcs:ignore-next-line Drupal.DeprecatedFunctions.GlobalFunction
62    $providers = \Drupal::moduleHandler()->invokeAll('display_builder_provider_info');
63
64    foreach ($providers as $provider) {
65      if (\str_starts_with($instance_id, $provider['prefix'])) {
66        return $provider['class']::getDisplayUrlFromInstanceId($instance_id);
67      }
68    }
69
70    return NULL;
71  }
72
73}