You are here

private function MailchimpListsSubscription::getFieldmapOptions in Mailchimp 8

Same name and namespace in other branches
  1. 2.x modules/mailchimp_lists/src/Plugin/Field/FieldType/MailchimpListsSubscription.php \Drupal\mailchimp_lists\Plugin\Field\FieldType\MailchimpListsSubscription::getFieldmapOptions()

Get an array with all possible Drupal properties for a given entity type.

Parameters

string $entity_type: Name of entity whose properties to list/audience.

string $entity_bundle: Optional bundle to limit available properties.

bool $required: Set to TRUE if properties are required.

string $prefix: Optional prefix for option IDs in the options list/audience.

string $tree: Optional name of the parent element if the options are part of a tree.

Return value

array List of properties that can be used as an #options list/audience.

1 call to MailchimpListsSubscription::getFieldmapOptions()
MailchimpListsSubscription::fieldSettingsForm in modules/mailchimp_lists/src/Plugin/Field/FieldType/MailchimpListsSubscription.php
Returns a form for the field-level settings.

File

modules/mailchimp_lists/src/Plugin/Field/FieldType/MailchimpListsSubscription.php, line 346

Class

MailchimpListsSubscription
Plugin implementation of the 'mailchimp_lists_subscription' field type.

Namespace

Drupal\mailchimp_lists\Plugin\Field\FieldType

Code

private function getFieldmapOptions($entity_type, $entity_bundle = NULL, $required = FALSE, $prefix = NULL, $tree = NULL) {
  $options = [];
  if (!$prefix) {
    $options[''] = $this
      ->t('-- Select --');
  }

  /** @var \Drupal\Core\Field\FieldDefinitionInterface[] $field_definitions */
  $field_definitions = \Drupal::service('entity_field.manager')
    ->getFieldDefinitions($entity_type, $entity_bundle);
  foreach ($field_definitions as $field_name => $field_definition) {
    $keypath = $prefix ? $prefix . ':' . $field_name : $field_name;
    $label = $field_definition
      ->getLabel();
    if ($field_definition
      ->getSetting('target_type')) {
      $target_type = $field_definition
        ->getSetting('target_type');
      $target_definition = \Drupal::entityTypeManager()
        ->getDefinition($target_type);

      // We offer fields on related fieldable entities (useful for field
      // collections).
      // But we only offer 1 level of depth to avoid loops.
      if ($target_definition
        ->entityClassImplements(FieldableEntityInterface::class) && !$prefix) {
        $handler_settings = $field_definition
          ->getSetting('handler_settings');
        $bundle = NULL;
        if ($target_definition
          ->hasKey('bundle')) {

          // @todo Support multiple target bundles?
          if (!empty($handler_settings['target_bundles']) && count($handler_settings['target_bundles']) == 1) {
            $bundle = reset($handler_settings['target_bundles']);
          }
        }
        else {
          $bundle = $target_type;
        }
        if ($bundle) {
          $options[(string) $label] = $this
            ->getFieldmapOptions($field_definition
            ->getSetting('target_type'), $bundle, $required, $keypath . ':entity', $label);
        }
      }
    }
    elseif (!$required || $field_definition
      ->isRequired() || $field_definition
      ->isComputed()) {

      // Get a list of non-computed property definitions.
      $property_definitions = $field_definition
        ->getFieldStorageDefinition()
        ->getPropertyDefinitions();
      $property_definitions = array_filter($property_definitions, function (DataDefinitionInterface $property_definition) {
        return !$property_definition
          ->isComputed();
      });
      if (count($property_definitions) > 1) {
        foreach ($property_definitions as $property => $property_definition) {
          $options[(string) $label][$keypath . ':' . $property] = $property_definition
            ->getLabel();
        }
      }
      else {
        $options[$keypath] = $label;
      }
    }
  }
  return $options;
}