function OptionsWidgetBase::getSelectedOptions

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsWidgetBase.php \Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsWidgetBase::getSelectedOptions()
  2. 10 core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsWidgetBase.php \Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsWidgetBase::getSelectedOptions()
  3. 11.x 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 174

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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.