ListWidget.php in Select (or other) 8
File
src/Plugin/Field/FieldWidget/ListWidget.php
View source
<?php
namespace Drupal\select_or_other\Plugin\Field\FieldWidget;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
class ListWidget extends WidgetBase {
public static function defaultSettings() {
return [
'add_other_value_to_allowed_values' => TRUE,
] + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state);
$form['add_other_value_to_allowed_values'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Add entered values from the other field to the allowed values list.'),
'#default_value' => $this
->getSetting('add_other_value_to_allowed_values'),
];
return $form;
}
protected function getOptions(FieldableEntityInterface $entity = NULL) {
$options = [];
if ($entity) {
$options = $this->fieldDefinition
->getFieldStorageDefinition()
->getOptionsProvider($this
->getColumn(), $entity)
->getSettableOptions(\Drupal::currentUser());
}
return $options;
}
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element = parent::formElement($items, $delta, $element, $form, $form_state);
if (!$this
->getSetting('add_other_value_to_allowed_values')) {
$element['#original_options'] = $element['#options'];
$element['#other_options'] = [];
$values = $items
->getValue();
if (!empty($values)) {
foreach ($values as $value) {
if (isset($value['value']) && !isset($element['#options'][$value['value']])) {
$element['#other_options'][] = $value['value'];
}
}
}
}
$element = $element + [
'#merged_values' => TRUE,
];
return $element;
}
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
unset($values['select']);
unset($values['other']);
if ($this
->getSetting('add_other_value_to_allowed_values')) {
$new_values = $this
->extractNewValues($values);
if (!empty($new_values)) {
$this
->addNewValuesToAllowedValues($new_values);
}
}
return parent::massageFormValues($values, $form, $form_state);
}
protected function extractNewValues(array $values) {
$allowed_values = $this->fieldDefinition
->getSetting('allowed_values');
$new_values = [];
foreach ($values as $value) {
if (!empty($value) && !isset($allowed_values[$value])) {
$new_values[] = $value;
}
}
return $new_values;
}
protected function addNewValuesToAllowedValues(array $values_to_add) {
$entity_type = $this->fieldDefinition
->getTargetEntityTypeId();
$field_name = $this->fieldDefinition
->getName();
$storage = \Drupal::entityTypeManager()
->getStorage('field_storage_config')
->load("{$entity_type}.{$field_name}");
$allowed_values = $storage
->getSetting('allowed_values');
foreach ($values_to_add as $value) {
if (!isset($allowed_values[$value])) {
$allowed_values[$value] = $value;
}
}
if ($allowed_values !== $storage
->getSetting('allowed_values')) {
$storage
->setSetting('allowed_values', $allowed_values)
->save();
drupal_static_reset('options_allowed_values');
}
}
}
Classes
Name |
Description |
ListWidget |
Plugin implementation of the 'select_or_other_list' widget. |