You are here

protected function WidgetBase::getSelectedOptions in Select (or other) 8

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

Determines selected options from the incoming field values.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The field values.

Return value

array The array of corresponding selected options.

1 call to WidgetBase::getSelectedOptions()
WidgetBase::formElement in src/Plugin/Field/FieldWidget/WidgetBase.php
Returns the form for a single field widget.

File

src/Plugin/Field/FieldWidget/WidgetBase.php, line 171

Class

WidgetBase
Base class for the 'select_or_other_*' widgets.

Namespace

Drupal\select_or_other\Plugin\Field\FieldWidget

Code

protected function getSelectedOptions(FieldItemListInterface $items) {
  $selected_options = [];
  foreach ($items as $item) {
    $column = $this
      ->getColumn();
    if ($value = $item
      ->get($column)
      ->getValue()) {
      $selected_options[] = $value;
    }
  }
  $selected_options = $this
    ->prepareSelectedOptions($selected_options);
  if ($selected_options) {

    // We need to check against a flat list of options.
    $flattened_options = $this
      ->flattenOptions($this
      ->getOptions($items
      ->getEntity()));
    foreach ($selected_options as $key => $selected_option) {

      // Remove the option if it does not exist in the options.
      if (!isset($flattened_options[$selected_option])) {
        unset($selected_options[$key]);
      }
    }
  }
  return $selected_options;
}