<?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 forenle_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;
}
