You are here

public function DynamicEntityReferenceItem::getSettableOptions in Dynamic Entity Reference 8.2

Same name and namespace in other branches
  1. 8 src/Plugin/Field/FieldType/DynamicEntityReferenceItem.php \Drupal\dynamic_entity_reference\Plugin\Field\FieldType\DynamicEntityReferenceItem::getSettableOptions()

To select both target_type and target_id the option value is changed from target_id to target_type-target_id.

Overrides EntityReferenceItem::getSettableOptions

See also

\Drupal\dynamic_entity_reference\Plugin\Field\FieldWidget\DynamicEntityReferenceOptionsTrait::massageFormValues()

File

src/Plugin/Field/FieldType/DynamicEntityReferenceItem.php, line 153

Class

DynamicEntityReferenceItem
Defines the 'dynamic_entity_reference' entity field type.

Namespace

Drupal\dynamic_entity_reference\Plugin\Field\FieldType

Code

public function getSettableOptions(AccountInterface $account = NULL) {
  $field_definition = $this
    ->getFieldDefinition();
  $entity_type_manager = \Drupal::entityTypeManager();
  $entity_type_bundles_info = \Drupal::service('entity_type.bundle.info');
  $selection_manager = \Drupal::service('plugin.manager.dynamic_entity_reference_selection');
  $options = [];
  $settings = $this
    ->getSettings();
  $target_types = static::getTargetTypes($settings);
  foreach ($target_types as $target_type) {
    $options[$target_type] = $selection_manager
      ->getSelectionHandler($field_definition, $this
      ->getEntity(), $target_type)
      ->getReferenceableEntities();
  }
  if (empty($options)) {
    return [];
  }
  $return = [];
  foreach ($options as $target_type => $referenceable_entities) {
    $target_type_info = $entity_type_manager
      ->getDefinition($target_type);
    $target_type_label = $target_type_info
      ->getLabel();

    // Rebuild the array by changing the bundle key into the bundle label.
    $bundles = $entity_type_bundles_info
      ->getBundleInfo($target_type);
    foreach ($referenceable_entities as $bundle => $entities) {

      // The label does not need sanitizing since it is used as an optgroup
      // which is only supported by select elements and auto-escaped.
      $bundle_label = $bundles[$bundle]['label'];
      foreach ($entities as $id => $entity_label) {
        if (count($target_types) > 1) {
          if ($target_type_info
            ->hasKey('bundle')) {
            $return[(string) $target_type_label . ': ' . (string) $bundle_label]["{$target_type}-{$id}"] = "{$entity_label} ({$target_type_label}:{$id})";
          }
          else {
            $return[(string) $target_type_label]["{$target_type}-{$id}"] = "{$entity_label} ({$target_type_label}:{$id})";
          }
        }
        else {
          $return[(string) $bundle_label]["{$target_type}-{$id}"] = "{$entity_label} ({$target_type_label}:{$id})";
        }
      }
    }
  }
  return $return;
}