You are here

public function EntityReferenceItem::getSettableOptions in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem::getSettableOptions()
  2. 10 core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem::getSettableOptions()

Returns an array of settable values with labels for display.

If the optional $account parameter is passed, then the array is filtered to values settable by the account.

Parameters

\Drupal\Core\Session\AccountInterface $account: (optional) The user account for which to filter the settable options. If omitted, all settable options are returned.

Return value

array An array of settable options for the object that may be used in an Options widget, usually when new data should be entered. It may either be a flat array of option labels keyed by values, or a two-dimensional array of option groups (array of flat option arrays, keyed by option group label). Note that labels should NOT be sanitized.

Overrides OptionsProviderInterface::getSettableOptions

2 calls to EntityReferenceItem::getSettableOptions()
EntityReferenceItem::getPossibleOptions in core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php
Returns an array of possible values with labels for display.
EntityReferenceItem::getSettableValues in core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php
Returns an array of settable values.

File

core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php, line 599

Class

EntityReferenceItem
Defines the 'entity_reference' entity field type.

Namespace

Drupal\Core\Field\Plugin\Field\FieldType

Code

public function getSettableOptions(AccountInterface $account = NULL) {
  $field_definition = $this
    ->getFieldDefinition();
  if (!($options = \Drupal::service('plugin.manager.entity_reference_selection')
    ->getSelectionHandler($field_definition, $this
    ->getEntity())
    ->getReferenceableEntities())) {
    return [];
  }

  // Rebuild the array by changing the bundle key into the bundle label.
  $target_type = $field_definition
    ->getSetting('target_type');
  $bundles = \Drupal::service('entity_type.bundle.info')
    ->getBundleInfo($target_type);
  $return = [];
  foreach ($options as $bundle => $entity_ids) {

    // 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 = (string) $bundles[$bundle]['label'];
    $return[$bundle_label] = $entity_ids;
  }
  return count($return) == 1 ? reset($return) : $return;
}