abstract class OptionsWidgetBase extends WidgetBase {
const OPTIONS_EMPTY_NONE = 'option_none';
const OPTIONS_EMPTY_SELECT = 'option_select';
protected $column;
public function __construct($plugin_id, array $plugin_definition, FieldInstance $instance, array $settings) {
parent::__construct($plugin_id, $plugin_definition, $instance, $settings);
reset($this->field['columns']);
$this->column = key($this->field['columns']);
}
public function formElement(array $items, $delta, array $element, $langcode, array &$form, array &$form_state) {
$this->entity = $element['#entity'];
$this->required = $element['#required'];
$this->multiple = $this->field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || $this->field['cardinality'] > 1;
$this->has_value = isset($items[0][$this->column]);
$element['#element_validate'][] = array(
get_class($this),
'validateElement',
);
$element['#key_column'] = $this->column;
return $element;
}
public static function validateElement(array $element, array &$form_state) {
if ($element['#required'] && $element['#value'] == '_none') {
form_error($element, t('!name field is required.', array(
'!name' => $element['#title'],
)));
}
if (is_array($element['#value'])) {
$values = array_values($element['#value']);
}
else {
$values = array(
$element['#value'],
);
}
$index = array_search('_none', $values, TRUE);
if ($index !== FALSE) {
unset($values[$index]);
}
$items = array();
foreach ($values as $value) {
$items[] = array(
$element['#key_column'] => $value,
);
}
form_set_value($element, $items, $form_state);
}
protected function getOptions() {
if (!isset($this->options)) {
$module_handler = \Drupal::moduleHandler();
$options = (array) $module_handler
->invoke($this->field['module'], 'options_list', array(
$this->field,
$this->instance,
$this->entity,
));
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',
));
if (!$this
->supportsGroups()) {
$options = $this
->flattenOptions($options);
}
$this->options = $options;
}
return $this->options;
}
protected function getSelectedOptions(array $items) {
$flat_options = $this
->flattenOptions($this
->getOptions());
$selected_options = array();
foreach ($items as $item) {
$value = $item[$this->column];
if (isset($flat_options[$value])) {
$selected_options[] = $value;
}
}
return $selected_options;
}
protected function flattenOptions(array $array) {
$result = array();
array_walk_recursive($array, function ($a, $b) use (&$result) {
$result[$b] = $a;
});
return $result;
}
protected function supportsGroups() {
return FALSE;
}
protected static function sanitizeLabel(&$label) {
$label = field_filter_xss($label);
}
protected function getEmptyOption() {
}
}