By swentel on
Change record status:
Published (View all published change records)
Project:
Introduced in branch:
8.x
Introduced in version:
8.x
Issue links:
Description:
In Drupal 7 you could override theme_options_none() to change the label of the "empty" (no selection) option.
/**
* Overrides theme_options_none().
*/
function MYTHEME_options_none($variables) {
// Check if this is the field we want to change.
if ($variables['instance']['field_name'] == 'field_option') {
// Change the label of the empty option.
return t('== Empty ==');
}
}
In Drupal 8, this can be done by an alter hook.
/**
* @param array $options
* The array of options for the field, as returned by hook_options_list(). An
* empty option (_none) might have been added, depending on the field
* properties.
*
* @param array $context
* An associative array containing:
* - field_definition: The field definition
* (\Drupal\Core\Field\FieldDefinitionInterface).
* - entity: The entity object the field is attached to
* (\Drupal\Core\Entity\EntityInterface).
*/
function hook_options_list_alter(array &$options, array $context) {
// Check if this is the field we want to change.
if ($context['field']->id() == 'node.article.field_option') {
// Change the label of the empty option.
$options['_none'] = t('== Empty ==');
}
}
Impacts:
Module developers
Themers