function OptionsAllowedValues::getAllowedValues

Same name and namespace in other branches
  1. 11.x core/modules/options/src/OptionsAllowedValues.php \Drupal\options\OptionsAllowedValues::getAllowedValues()

File

core/modules/options/src/OptionsAllowedValues.php, line 31

Class

OptionsAllowedValues
Provides allowed values for a list field.

Namespace

Drupal\options

Code

public function getAllowedValues(FieldStorageDefinitionInterface $definition, ?FieldableEntityInterface $entity = NULL) : array {
  $cid = $this->getCid([
    $definition->getTargetEntityTypeId(),
    $definition->getName(),
    $entity ? 'entity' : 'any',
  ]);
  if ($cache = $this->cache
    ->get($cid)) {
    return $cache->data;
  }
  $callback = $definition->getSetting('allowed_values_function');
  // If $cacheable is FALSE, then the allowed values are not cached.
  // \Drupal\options_test\OptionsAllowedValues::dynamicValues() offers an
  // example of generating dynamic and uncached values.
  // @see \Drupal\options_test\OptionsAllowedValues::dynamicValues()
  $cacheable = TRUE;
  if (!empty($callback) && is_callable($callback)) {
    $allowedValues = $callback($definition, $entity, $cacheable);
  }
  else {
    $allowedValues = $definition->getSetting('allowed_values') ?? [];
  }
  if ($cacheable) {
    $tags = Cache::mergeTags($definition->getCacheTags(), [
      static::CACHE_TAG,
    ]);
    $this->cache
      ->set($cid, $allowedValues, CacheBackendInterface::CACHE_PERMANENT, $tags);
  }
  return $allowedValues;
}

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