Drupal 9: Render available select options in paragraph template

raw

0-mytheme.theme.php

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;
}
 
raw

1-paragraph--logomap-filter.html.twig

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
{% set show_partner_type = content.field_filter_show_partner_type[0]['#markup'] %}
{% set show_topic = content.field_filter_show_topic[0]['#markup'] %}
 
{% set partner_type_default = '' %}
{% for key in paragraph.field_filter_partner_type|keys %}
  {% set partner_type_default = paragraph.field_filter_partner_type[key].value %}
{% endfor %}
 
{% set topic_defaults = [] %}
{% for num in content.field_filter_topic['#items']|keys %}
  {% set topic_defaults = topic_defaults|merge([content.field_filter_topic[num]['#options']['entity'].id]) %}
{% endfor %}
 
 
<div class="row">
  {% if show_partner_type == 1 %}
    {% set partner_type_options = elements.field_filter_partner_type['#lfoptions'] %}
    <select name="partner_type" class="input-field__input">
      <option value="">- alle -</option>
      {% for key, value in partner_type_options %}
        <option value="{{key}}"{% if key == partner_type_default %} selected="selected"{% endif %}>{{value}}</option>
      {% endfor %}
    </select>
  {% else %}
    <input type="hidden" name="partner_type" value="{{partner_type_default}}">
  {% endif %}
</div>
 
<div class="row">
  {% if show_topic == '1' %}
    {% set topic_options = elements.field_filter_topic['#lfoptions'] %}
    <select name="filter_topics[]" multiple="multiple" class="basic-multiple-select">
      {% for key, value in topic_options %}
        <option value="{{key}}"{% if key in topic_defaults %} selected="selected"{% endif %}>{{value}}</option>
      {% endfor %}
    </select>
  {% else %}
    <input type="hidden" name="filter_topics[]" value="{{topic_defaults|join(',')}}">
  {% endif %}
</div>
 
 
Christian Weiske Christian Weiske
owner

History