You are here

protected function Select2EntityReferenceWidget::getOptions in Select 2 8

Returns the array of options for the widget.

Parameters

\Drupal\Core\Entity\FieldableEntityInterface $entity: The entity for which to return options.

Return value

array The array of options for the widget.

Overrides OptionsWidgetBase::getOptions

File

src/Plugin/Field/FieldWidget/Select2EntityReferenceWidget.php, line 110

Class

Select2EntityReferenceWidget
Plugin implementation of the 'select2' widget.

Namespace

Drupal\select2\Plugin\Field\FieldWidget

Code

protected function getOptions(FieldableEntityInterface $entity) {
  if (!isset($this->options) && $this
    ->getSetting('autocomplete')) {

    // Get all currently selected options.
    $selected_options = [];
    foreach ($entity
      ->get($this->fieldDefinition
      ->getName()) as $item) {
      if ($item->{$this->column} !== NULL) {
        $selected_options[] = $item->{$this->column};
      }
    }
    if (!$selected_options) {
      return $this->options = [];
    }

    // Validate that the options are matching the target_type and handler
    // settings.
    $handler_settings = $this
      ->getSelectionSettings() + [
      'target_type' => $this
        ->getFieldSetting('target_type'),
      'handler' => $this
        ->getFieldSetting('handler'),
    ];
    return $this->options = static::getValidReferenceableEntities($selected_options, $handler_settings);
  }
  return parent::getOptions($entity);
}