options.module
Same filename in other branches
File
-
core/
modules/ options/ options.module
View source
<?php
/**
* @file
*/
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
/**
* 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.
*
* @param \Drupal\Core\Field\FieldStorageDefinitionInterface $definition
* The field storage definition.
* @param \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 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 callback_allowed_values_function()
*/
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];
}
/**
* Checks if a list of values are being used in actual field values.
*/
function _options_values_in_use($entity_type, $field_name, $values) {
if ($values) {
$result = \Drupal::entityQuery($entity_type)->condition($field_name . '.value', $values, 'IN')
->count()
->accessCheck(FALSE)
->range(0, 1)
->execute();
if ($result) {
return TRUE;
}
}
return FALSE;
}
Functions
Title | Deprecated | Summary |
---|---|---|
options_allowed_values | Returns the array of allowed values for a list field. | |
_options_values_in_use | Checks if a list of values are being used in actual field values. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.