You are here

protected function ListWidget::addNewValuesToAllowedValues in Select (or other) 8

Same name and namespace in other branches
  1. 4.x src/Plugin/Field/FieldWidget/ListWidget.php \Drupal\select_or_other\Plugin\Field\FieldWidget\ListWidget::addNewValuesToAllowedValues()

Adds new values to the allowed values for this field.

Parameters

array $values_to_add: The values to add to the allowed values.

1 call to ListWidget::addNewValuesToAllowedValues()
ListWidget::massageFormValues in src/Plugin/Field/FieldWidget/ListWidget.php
Massages the form values into the format expected for field values.

File

src/Plugin/Field/FieldWidget/ListWidget.php, line 135

Class

ListWidget
Plugin implementation of the 'select_or_other_list' widget.

Namespace

Drupal\select_or_other\Plugin\Field\FieldWidget

Code

protected function addNewValuesToAllowedValues(array $values_to_add) {
  $entity_type = $this->fieldDefinition
    ->getTargetEntityTypeId();
  $field_name = $this->fieldDefinition
    ->getName();

  /** @var \Drupal\field\FieldStorageConfigInterface $storage */
  $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');
  }
}