You are here

protected function ReferenceWidget::prepareSelectedOptions in Select (or other) 8

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

Prepares selected options for comparison to the available options.

Sometimes widgets have to change the keys of their available options. This method allows those widgets to do the same with the selected options to ensure they actually end up selected in the widget.

Parameters

array $options: The options to prepare.

Return value

array The prepared option.

Overrides WidgetBase::prepareSelectedOptions

File

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

Class

ReferenceWidget
Plugin implementation of the 'select_or_other_reference' widget.

Namespace

Drupal\select_or_other\Plugin\Field\FieldWidget

Code

protected function prepareSelectedOptions(array $options) {
  $prepared_options = [];
  $entities = $this
    ->getEntityStorage()
    ->loadMultiple($options);
  foreach ($entities as $entity) {
    $prepared_options[] = "{$entity->label()} ({$entity->id()})";
  }
  return $prepared_options;
}