function hook_options_list_alter
Same name in other branches
- 9 core/modules/options/options.api.php \hook_options_list_alter()
- 8.9.x core/modules/options/options.api.php \hook_options_list_alter()
- 11.x core/modules/options/options.api.php \hook_options_list_alter()
Alters the list of options to be displayed for a field.
This hook can notably be used to change the label of the empty option.
Parameters
array $options: The array of options for the field, as returned by \Drupal\Core\TypedData\OptionsProviderInterface::getSettableOptions(). An empty option (_none) might have been added, depending on the field properties.
array $context: An associative array containing:
- fieldDefinition: The field definition (\Drupal\Core\Field\FieldDefinitionInterface).
- entity: The entity object the field is attached to (\Drupal\Core\Entity\EntityInterface).
- widget: The widget object (\Drupal\Core\Field\WidgetInterface).
See also
hook_options_list()
Related topics
1 function implements hook_options_list_alter()
Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.
- options_test_options_list_alter in core/
modules/ options/ tests/ options_test/ options_test.module - Implements hook_options_list_alter().
1 invocation of hook_options_list_alter()
- OptionsWidgetBase::getOptions in core/
lib/ Drupal/ Core/ Field/ Plugin/ Field/ FieldWidget/ OptionsWidgetBase.php - Returns the array of options for the widget.
File
-
core/
modules/ options/ options.api.php, line 32
Code
function hook_options_list_alter(array &$options, array $context) {
// Check if this is the field we want to change.
if ($context['fieldDefinition']->getName() == 'field_option') {
// Change the label of the empty option.
$options['_none'] = t('== Empty ==');
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.