You are here

protected function OptionsWidgetBase::getSelectedOptions in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsWidgetBase.php \Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsWidgetBase::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.

2 calls to OptionsWidgetBase::getSelectedOptions()
OptionsButtonsWidget::formElement in core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsButtonsWidget.php
Returns the form for a single field widget.
OptionsSelectWidget::formElement in core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsSelectWidget.php
Returns the form for a single field widget.

File

core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsWidgetBase.php, line 152

Class

OptionsWidgetBase
Base class for the 'options_*' widgets.

Namespace

Drupal\Core\Field\Plugin\Field\FieldWidget

Code

protected function getSelectedOptions(FieldItemListInterface $items) {

  // We need to check against a flat list of options.
  $flat_options = OptGroup::flattenOptions($this
    ->getOptions($items
    ->getEntity()));
  $selected_options = [];
  foreach ($items as $item) {
    $value = $item->{$this->column};

    // Keep the value if it actually is in the list of options (needs to be
    // checked against the flat list).
    if (isset($flat_options[$value])) {
      $selected_options[] = $value;
    }
  }
  return $selected_options;
}