You are here

protected function DynamicEntityReferenceOptionsTrait::getSelectedOptions in Dynamic Entity Reference 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldWidget/DynamicEntityReferenceOptionsTrait.php \Drupal\dynamic_entity_reference\Plugin\Field\FieldWidget\DynamicEntityReferenceOptionsTrait::getSelectedOptions()

File

src/Plugin/Field/FieldWidget/DynamicEntityReferenceOptionsTrait.php, line 30

Class

DynamicEntityReferenceOptionsTrait
The common functionality between DynamicEntityReferenceOptionsWidgets.

Namespace

Drupal\dynamic_entity_reference\Plugin\Field\FieldWidget

Code

protected function getSelectedOptions(FieldItemListInterface $items, $delta = 0) {

  // 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->target_type}-{$item->target_id}";

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