function options_allowed_values

Same name in other branches
  1. 9 core/modules/options/options.module \options_allowed_values()
  2. 8.9.x core/modules/options/options.module \options_allowed_values()
  3. 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\FieldFilteredMarkup 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/Kernel/OptionsDynamicValuesApiTest.php
Tests options_allowed_values().

... See full list

3 string references to 'options_allowed_values'
ListItemBase::submitFieldStorageUpdate in core/modules/options/src/Plugin/Field/FieldType/ListItemBase.php
Resets the static variable on field storage update.
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

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];
}

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