You are here

function options_allowed_values in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/options/options.module \options_allowed_values()
  2. 10 core/modules/options/options.module \options_allowed_values()

Returns the array of allowed values for a list field.

The strings are not safe for output. Keys and values of the array should be sanitized through \Drupal\Core\Field\AllowedTagsXssTrait::fieldFilterXss() before being displayed.

Parameters

\Drupal\Core\Field\FieldStorageDefinitionInterface $definition: The field storage definition.

\Drupal\Core\Entity\FieldableEntityInterface|null $entity: (optional) The specific entity when this function is called from the context of a specific field on a specific entity. This allows custom 'allowed_values_function' callbacks to either restrict the values or customize the labels for particular bundles and entities. NULL when there is not a specific entity available, such as for Views filters.

Return value

array The array of allowed values. Keys of the array are the raw stored values (number or text), values of the array are the display labels.

See also

callback_allowed_values_function()

6 calls to options_allowed_values()
ListField::init in core/modules/options/src/Plugin/views/filter/ListField.php
Overrides \Drupal\views\Plugin\views\HandlerBase::init().
ListItemBase::generateSampleValue in core/modules/options/src/Plugin/Field/FieldType/ListItemBase.php
Generates placeholder field values.
ListItemBase::getSettableOptions in core/modules/options/src/Plugin/Field/FieldType/ListItemBase.php
Returns an array of settable values with labels for display.
NumberListField::init in core/modules/options/src/Plugin/views/argument/NumberListField.php
Overrides Drupal\views\Plugin\views\HandlerBase:init().
OptionsDynamicValuesApiTest::testOptionsAllowedValues in core/modules/options/tests/src/Functional/OptionsDynamicValuesApiTest.php
Tests options_allowed_values().

... See full list

2 string references to 'options_allowed_values'
options_field_storage_config_delete in core/modules/options/options.module
Implements hook_ENTITY_TYPE_delete() for 'field_storage_config'.
options_field_storage_config_update in core/modules/options/options.module
Implements hook_ENTITY_TYPE_update() for 'field_storage_config'.

File

core/modules/options/options.module, line 73
Defines selection, check box and radio button widgets for text and numeric fields.

Code

function options_allowed_values(FieldStorageDefinitionInterface $definition, FieldableEntityInterface $entity = NULL) {
  $allowed_values =& drupal_static(__FUNCTION__, []);
  $cache_keys = [
    $definition
      ->getTargetEntityTypeId(),
    $definition
      ->getName(),
  ];
  if ($entity) {
    $cache_keys[] = 'entity';
  }
  $cache_id = implode(':', $cache_keys);
  if (!isset($allowed_values[$cache_id])) {
    $function = $definition
      ->getSetting('allowed_values_function');

    // If $cacheable is FALSE, then the allowed values are not statically
    // cached. See options_test_dynamic_values_callback() for an example of
    // generating dynamic and uncached values.
    $cacheable = TRUE;
    if (!empty($function)) {
      $values = $function($definition, $entity, $cacheable);
    }
    else {
      $values = $definition
        ->getSetting('allowed_values');
    }
    if ($cacheable) {
      $allowed_values[$cache_id] = $values;
    }
    else {
      return $values;
    }
  }
  return $allowed_values[$cache_id];
}