Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 31 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
ExtraFieldSource | |
0.00% |
0 / 31 |
|
0.00% |
0 / 5 |
72 | |
0.00% |
0 / 1 |
defaultSettings | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
settingsSummary | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
getPropValue | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
settingsForm | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
6 | |||
getDefinitions | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace Drupal\display_builder_entity_view\Plugin\UiPatterns\Source; |
6 | |
7 | use Drupal\Core\Form\FormStateInterface; |
8 | use Drupal\Core\Plugin\Context\ContextDefinition; |
9 | use Drupal\Core\StringTranslation\TranslatableMarkup; |
10 | use Drupal\display_builder\RenderableBuilderTrait; |
11 | use Drupal\ui_patterns\Attribute\Source; |
12 | use Drupal\ui_patterns\SourcePluginBase; |
13 | |
14 | /** |
15 | * Plugin implementation of the source. |
16 | */ |
17 | #[Source( |
18 | id: 'extra_field', |
19 | label: new TranslatableMarkup('Extra field'), |
20 | description: new TranslatableMarkup('❌ Used only for imports from Manage Display and Layout Builder. Hidden from Library.'), |
21 | prop_types: ['slot'], |
22 | context_definitions: [ |
23 | 'entity' => new ContextDefinition('entity', label: new TranslatableMarkup('Entity'), required: TRUE), |
24 | ], |
25 | )] |
26 | class ExtraFieldSource extends SourcePluginBase { |
27 | |
28 | use RenderableBuilderTrait; |
29 | |
30 | /** |
31 | * {@inheritdoc} |
32 | */ |
33 | public function defaultSettings(): array { |
34 | return [ |
35 | 'field' => NULL, |
36 | ]; |
37 | } |
38 | |
39 | /** |
40 | * {@inheritdoc} |
41 | */ |
42 | public function settingsSummary(): array { |
43 | return [ |
44 | $this->getDefinitions()[$this->getSetting('field')]['label'] ?? '', |
45 | ]; |
46 | } |
47 | |
48 | /** |
49 | * {@inheritdoc} |
50 | */ |
51 | public function getPropValue(): mixed { |
52 | $field = $this->getSetting('field'); |
53 | |
54 | if (!$field) { |
55 | return []; |
56 | } |
57 | $definition = $this->getDefinitions()[$field]; |
58 | |
59 | if (!$definition) { |
60 | return []; |
61 | } |
62 | |
63 | $label = $definition['label'] ?? ''; |
64 | $build = $this->buildPlaceholderButton($this->t('Extra field: @field', ['@field' => $label])); |
65 | $build['#attributes']['class'][] = 'db-background'; |
66 | |
67 | return $build; |
68 | } |
69 | |
70 | /** |
71 | * {@inheritdoc} |
72 | */ |
73 | public function settingsForm(array $form, FormStateInterface $form_state): array { |
74 | $options = []; |
75 | |
76 | foreach ($this->getDefinitions() as $field_name => $definition) { |
77 | $options[$field_name] = $definition['label']; |
78 | } |
79 | $form['field'] = [ |
80 | '#type' => 'select', |
81 | '#options' => $options, |
82 | '#default_value' => $this->getSetting('field'), |
83 | '#description' => $this->t('⚠️ Imported but not supported'), |
84 | ]; |
85 | |
86 | return $form; |
87 | } |
88 | |
89 | /** |
90 | * Get extra field definitions. |
91 | * |
92 | * Extra fields are not plugins but old-fashioned hooks. |
93 | * |
94 | * @throws \Drupal\Component\Plugin\Exception\ContextException |
95 | * |
96 | * @return array |
97 | * A list of extra field definition. |
98 | */ |
99 | protected function getDefinitions(): array { |
100 | $extra_fields = $this->moduleHandler->invokeAll('entity_extra_field_info'); |
101 | |
102 | /** @var \Drupal\Core\Entity\FieldableEntityInterface $entity */ |
103 | $entity = $this->getContextValue('entity'); |
104 | $entity_type = $entity->getEntityTypeId(); |
105 | $bundle_id = $entity->bundle(); |
106 | |
107 | return $extra_fields[$entity_type][$bundle_id]['display'] ?? []; |
108 | } |
109 | |
110 | } |