W3cubDocs

/Drupal 8

function options_allowed_values

options_allowed_values(FieldStorageDefinitionInterface $definition, FieldableEntityInterface $entity = NULL)

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()

File

core/modules/options/options.module, line 72
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__, array());

  $cache_keys = array($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];
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!modules!options!options.module/function/options_allowed_values/8.1.x