function FieldStorageConfig::getOptionsProvider

Same name and namespace in other branches
  1. 9 core/modules/field/src/Entity/FieldStorageConfig.php \Drupal\field\Entity\FieldStorageConfig::getOptionsProvider()
  2. 8.9.x core/modules/field/src/Entity/FieldStorageConfig.php \Drupal\field\Entity\FieldStorageConfig::getOptionsProvider()
  3. 11.x core/modules/field/src/Entity/FieldStorageConfig.php \Drupal\field\Entity\FieldStorageConfig::getOptionsProvider()

Gets an options provider for the given field item property.

Parameters

string $property_name: The name of the property to get options for; e.g., 'value'.

\Drupal\Core\Entity\FieldableEntityInterface $entity: The entity for which the options should be provided.

Return value

\Drupal\Core\TypedData\OptionsProviderInterface|null An options provider, or NULL if no options are defined.

Overrides FieldStorageDefinitionInterface::getOptionsProvider

File

core/modules/field/src/Entity/FieldStorageConfig.php, line 683

Class

FieldStorageConfig
Defines the Field storage configuration entity.

Namespace

Drupal\field\Entity

Code

public function getOptionsProvider($property_name, FieldableEntityInterface $entity) {
  // If the field item class implements the interface, create an orphaned
  // runtime item object, so that it can be used as the options provider
  // without modifying the entity being worked on.
  if (is_subclass_of($this->getFieldItemClass(), OptionsProviderInterface::class)) {
    try {
      $items = $entity->get($this->getName());
    } catch (\InvalidArgumentException $e) {
      // When a field doesn't exist, create a new field item list using a
      // temporary base field definition. This step is necessary since there
      // may not be a field configuration for the storage when creating a new
      // field.
      // @todo Simplify in https://www.drupal.org/project/drupal/issues/3347291.
      $field_storage = BaseFieldDefinition::createFromFieldStorageDefinition($this);
      $entity_adapter = EntityAdapter::createFromEntity($entity);
      $items = \Drupal::typedDataManager()->create($field_storage, name: $field_storage->getName(), parent: $entity_adapter);
    }
    return \Drupal::service('plugin.manager.field.field_type')->createFieldItem($items, 0);
  }
  // @todo Allow setting custom options provider.
  //   https://www.drupal.org/node/2002138.
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.