Issue #1898434 by gnuget, joelpittet, webthingee, -enzo-, Cottser: Remove theme_options_none(), use an alter hook instead for changing empty option label.

8.0.x
Nathaniel Catchpole 2013-05-05 22:32:01 +01:00
parent 3d1da5ab8f
commit be3667b3ef
3 changed files with 55 additions and 52 deletions

View File

@ -112,8 +112,33 @@ abstract class OptionsWidgetBase extends WidgetBase {
*/
protected function getOptions() {
if (!isset($this->options)) {
$module_handler = \Drupal::moduleHandler();
// Get the list of options from the field type module, and sanitize them.
$options = (array) module_invoke($this->field['module'], 'options_list', $this->field, $this->instance, $this->entity);
$options = (array) $module_handler->invoke($this->field['module'], 'options_list', array($this->field, $this->instance, $this->entity));
// Add an empty option if the widget needs one.
if ($empty_option = $this->getEmptyOption()) {
switch ($this->getPluginId()) {
case 'options_buttons':
$label = t('N/A');
break;
case 'options_select':
$label = ($empty_option == static::OPTIONS_EMPTY_NONE ? t('- None -') : t('- Select a value -'));
break;
}
$options = array('_none' => $label) + $options;
}
$context = array(
'field' => $this->field,
'instance' => $this->instance,
'entity' => $this->entity,
);
$module_handler->alter('options_list', $options, $context);
array_walk_recursive($options, array($this, 'sanitizeLabel'));
// Options might be nested ("optgroups"). If the widget does not support
@ -122,12 +147,6 @@ abstract class OptionsWidgetBase extends WidgetBase {
$options = $this->flattenOptions($options);
}
// Add an empty option if the widget needs one.
if ($empty_option = $this->getEmptyOption()) {
$label = theme('options_none', array('option' => $empty_option, 'widget' => $this, 'instance' => $this->instance));
$options = array('_none' => $label) + $options;
}
$this->options = $options;
}
return $this->options;

View File

@ -68,3 +68,32 @@ function hook_options_list($field, $instance, $entity) {
return $options;
}
/**
* 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.
*
* @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: The field definition (\Drupal\field\Plugin\Core\Entity\Field).
* - instance: The instance definition. It is recommended to only use instance
* level properties to filter out values from a list defined by field level
* properties (Drupal\field\Plugin\Core\Entity\FieldInstance).
* - entity: The entity object the field is attached to
* (\Drupal\Core\Entity\EntityInterface).
*
* @see hook_options_list()
*/
function hook_options_list_alter(array &$options, array $context) {
// Check if this is the field we want to change.
if ($context['field']->id == 'field_option') {
// Change the label of the empty option.
$options['_none'] = t('== Empty ==');
}
}

View File

@ -23,17 +23,6 @@ function options_help($path, $arg) {
}
}
/**
* Implements hook_theme().
*/
function options_theme() {
return array(
'options_none' => array(
'variables' => array('instance' => NULL, 'option' => NULL),
),
);
}
/**
* Implements hook_field_info().
*/
@ -444,37 +433,3 @@ function options_options_list($field, $instance, $entity) {
return options_allowed_values($field, $instance, $entity);
}
/**
* Returns HTML for the label for the empty value for non-required options.
*
* The default theme will display 'N/A' for a radio list and '- None -' or
* 'Select a value' for a select.
*
* @param $variables
* An associative array containing:
* - option: A string representing the option that should be displayed. Either
* \Drupal\options\Plugin\field\widget\OptionsWidgetBase::OPTIONS_EMPTY_NONE
* or
* \Drupal\options\Plugin\field\widget\OptionsWidgetBase::OPTIONS_EMPTY_SELECT.
* - widget: The widget object requesting the option.
* - instance: The instance definition.
*
* @ingroup themeable
*/
function theme_options_none($variables) {
$option = $variables['option'];
$widget = $variables['widget'];
$output = '';
switch ($widget->getPluginId()) {
case 'options_buttons':
$output = t('N/A');
break;
case 'options_select':
$output = ($option == OptionsWidgetBase::OPTIONS_EMPTY_NONE ? t('- None -') : t('- Select a value -'));
break;
}
return $output;
}