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