1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
| <?php
function mytheme_preprocess_paragraph(&$variables) {
if ($variables['paragraph']->getType() == 'logomap_filter') {
mytheme_preprocess_paragraph_logomap_filter($variables);
}
}
/**
* Make logomap_filter select field options available in the frontend
* as elements[$fieldname]['#lfoptions']
*/
function mytheme_preprocess_paragraph_logomap_filter(&$variables) {
$selectFields = [
'field_filter_partner_type', //string_list
'field_filter_topic', //entity_reference
];
foreach ($selectFields as $fieldName) {
$fieldConf = $variables['paragraph']->getFieldDefinition($fieldName);
$variables['elements'][$fieldName]['#lfoptions'] = mytheme_loadSelectOptions($fieldConf);
}
}
/**
* Get the available options for a select field (string list or entity reference)
*/
function mytheme_loadSelectOptions(FieldConfig $fieldConf): array {
switch ($fieldConf->getType()) {
case 'list_string':
return options_allowed_values($fieldConf->getFieldStorageDefinition());
case 'entity_reference':
return mytheme_loadEntityReferenceOptions(
$fieldConf->getSetting('target_type'),
$fieldConf->getSetting('handler_settings')['target_bundles']
);
default:
throw new \Exception('Unknown field type: ' . $fieldConf->getType());
}
}
/**
* Get the available options for an entity reference field
*
* @param string $target_type E.g. "taxonomy_term", "node"
* @param array $target_bundles E.g. ['tags' => 'tags'] or ['partner' => 'partner']
*
* @return array Key-value pairs (ID and label)
*/
function mytheme_loadEntityReferenceOptions(string $target_type, array $target_bundles): array {
$entity_type = \Drupal::entityTypeManager()->getDefinition($target_type);
$storage = \Drupal::entityTypeManager()->getStorage($target_type);
$query = $storage->getQuery();
$query->condition($entity_type->getKey('bundle'), $target_bundles, 'IN');
$entity_ids = $query->execute();
$entities = $storage->loadMultiple($entity_ids);
$options = [];
foreach ($entities as $entity) {
$options[$entity->id()] = $entity->label();
}
asort($options, SORT_STRING | SORT_FLAG_CASE);
return $options;
}
|