class OptionsAllowedValues
Same name in this branch
- 11.x core/modules/options/tests/options_test/src/OptionsAllowedValues.php \Drupal\options_test\OptionsAllowedValues
Same name and namespace in other branches
- main core/modules/options/tests/options_test/src/OptionsAllowedValues.php \Drupal\options_test\OptionsAllowedValues
- main core/modules/options/src/OptionsAllowedValues.php \Drupal\options\OptionsAllowedValues
Provides allowed values for a list field.
Hierarchy
- class \Drupal\options\OptionsAllowedValues implements \Drupal\options\OptionsAllowedValuesInterface
Expanded class hierarchy of OptionsAllowedValues
1 file declares its use of OptionsAllowedValues
- ListItemBase.php in core/
modules/ options/ src/ Plugin/ Field/ FieldType/ ListItemBase.php
1 string reference to 'OptionsAllowedValues'
- options.services.yml in core/
modules/ options/ options.services.yml - core/modules/options/options.services.yml
1 service uses OptionsAllowedValues
- Drupal\options\OptionsAllowedValuesInterface in core/
modules/ options/ options.services.yml - Drupal\options\OptionsAllowedValues
File
-
core/
modules/ options/ src/ OptionsAllowedValues.php, line 16
Namespace
Drupal\optionsView source
class OptionsAllowedValues implements OptionsAllowedValuesInterface {
/**
* Cache tag set on all cached allowed values.
*/
const CACHE_TAG = 'options_allowed_values';
public function __construct(#[Autowire(service: 'cache.memory')] protected CacheBackendInterface $cache) {
}
/**
* {@inheritdoc}
*/
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;
}
/**
* Returns the CID used to cache the options allowed values.
*
* @param array $path
* The parts identifying the allowed values: the target entity type ID,
* the field name and whether an entity is available.
*
* @return string
* The cache ID.
*/
protected function getCid(array $path) : string {
return 'options.allowed_values:' . implode(':', $path);
}
}
Members
| Title Sort descending | Modifiers | Object type | Summary |
|---|---|---|---|
| OptionsAllowedValues::CACHE_TAG | constant | Cache tag set on all cached allowed values. | |
| OptionsAllowedValues::getAllowedValues | public | function | |
| OptionsAllowedValues::getCid | protected | function | Returns the CID used to cache the options allowed values. |
| OptionsAllowedValues::__construct | public | function |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.