Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 21
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 / 21
0.00% covered (danger)
0.00%
0 / 2
42
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
2
 getLabelWithSummary
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
30
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 */
13class SlotSourceProxy {
14
15  public function __construct(
16    protected PluginManagerInterface $sourceManager,
17  ) {}
18
19  /**
20   * Get the label from data, with summary.
21   *
22   * @param array $data
23   *   The data to processed.
24   * @param \Drupal\Core\Plugin\Context\ContextInterface[] $contexts
25   *   (Optional) The contexts, keyed by context name.
26   *
27   * @return array{label: string, summary: string}
28   *   Array with keys 'label' and 'summary'.
29   */
30  public function getLabelWithSummary(array $data, array $contexts = []): array {
31    /** @var \Drupal\ui_patterns\SourcePluginManager $sourceManager */
32    $sourceManager = $this->sourceManager;
33    $source = $sourceManager->getSource($data['_node_id'] ?? '', [], $data, $contexts);
34
35    if (!$source) {
36      return [
37        'label' => '',
38        'summary' => '',
39      ];
40    }
41    $label = (string) $source->label();
42
43    $summary = $source->settingsSummary();
44    $labelSummary = $label;
45
46    if (\is_array($summary)) {
47      $summary = \array_map(static fn ($v) => \trim((string) $v), $summary);
48
49      if (!empty(\array_filter(\array_values($summary)))) {
50        $labelSummary .= ': ' . \implode(', ', $summary);
51      }
52    }
53    elseif (\is_string($summary)) {
54      $labelSummary .= ': ' . \trim($summary);
55    }
56
57    return [
58      'label' => $label,
59      // Block token can contain markup.
60      'summary' => Html::escape($labelSummary),
61    ];
62  }
63
64}