Code Coverage
 
Lines
Branches
Paths
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
SlotSourceProxy
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 2
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
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
 getLabelWithSummary
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2
3declare(strict_types=1);
4
5namespace Drupal\display_builder;
6
7use Drupal\Component\Plugin\PluginManagerInterface;
8use Drupal\Component\Utility\Html;
9
10/**
11 * Provide methods missing in UI Patterns.
12 *
13 * @todo remove when moved to UI Patterns #3568363.
14 */
15class SlotSourceProxy {
16
17  public function __construct(
18    protected PluginManagerInterface $sourceManager,
19  ) {}
20
21  /**
22   * Get the label from data, with summary.
23   *
24   * @param array $data
25   *   The data to processed.
26   * @param \Drupal\Core\Plugin\Context\ContextInterface[] $contexts
27   *   (Optional) The contexts, keyed by context name.
28   * @param bool $label_only
29   *   (Optional) Return label only and no summary. Default: false.
30   *
31   * @return array{label: string, summary: string}
32   *   Array with keys 'label' and 'summary'.
33   */
34  public function getLabelWithSummary(array $data, array $contexts = [], bool $label_only = FALSE): array {
35    /** @var \Drupal\ui_patterns\SourcePluginManager $sourceManager */
36    $sourceManager = $this->sourceManager;
37    $source = $sourceManager->getSource($data['node_id'] ?? '', [], $data, $contexts);
38
39    if (!$source) {
40      return [
41        'label' => '',
42        'summary' => '',
43      ];
44    }
45    $label = (string) $source->label(TRUE);
46
47    if ($label_only) {
48      return [
49        'label' => $label,
50        'summary' => '',
51      ];
52    }
53
54    $summary = $source->settingsSummary();
55    $labelSummary = $label;
56
57    if (\is_array($summary)) {
58      $summary = \array_map(static fn ($v) => \trim((string) $v), $summary);
59
60      if (!empty(\array_filter(\array_values($summary)))) {
61        $labelSummary .= ': ' . \implode(', ', $summary);
62      }
63    }
64    elseif (\is_string($summary)) {
65      $labelSummary .= ': ' . \trim($summary);
66    }
67
68    return [
69      'label' => $label,
70      // Block token can contain markup.
71      'summary' => Html::escape($labelSummary),
72    ];
73  }
74
75}