You are here

protected static function Select2Trait::getValidReferenceableEntities in Select 2 8

Validates an array of IDs.

Parameters

array $ids: Array of entity IDs.

array $handler_settings: Handler settings to load a selection plugin.

Return value

array Key => entity ID, Value => entity label.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

3 calls to Select2Trait::getValidReferenceableEntities()
Select2::getValidSelectedOptions in src/Element/Select2.php
Get an array of currently selected options.
Select2EntityReferenceWidget::getOptions in src/Plugin/Field/FieldWidget/Select2EntityReferenceWidget.php
Returns the array of options for the widget.
Select2EntityReferenceWidget::prepareFieldValues in src/Plugin/Field/FieldWidget/Select2EntityReferenceWidget.php
Set's the values to the correct column key.

File

src/Select2Trait.php, line 26

Class

Select2Trait
Trait with functions that are used in the element and the field widget.

Namespace

Drupal\select2

Code

protected static function getValidReferenceableEntities(array $ids, array $handler_settings) {
  $options = [];
  $valid_ids = \Drupal::service('plugin.manager.entity_reference_selection')
    ->getInstance($handler_settings)
    ->validateReferenceableEntities($ids);
  $entities = \Drupal::entityTypeManager()
    ->getStorage($handler_settings['target_type'])
    ->loadMultiple($valid_ids);
  foreach ($entities as $entity_id => $entity) {
    $options[$entity_id] = Html::decodeEntities(\Drupal::service('entity.repository')
      ->getTranslationFromContext($entity)
      ->label());
  }
  return $options;
}