You are here

protected function PreloadBuildTrait::buildPreLoaded in Select2 Boxes 8

Build preloaded entries list.

Parameters

string $count: Number of entries will be preloaded, or empty string to load all.

\Drupal\Core\Field\FieldDefinitionInterface $fieldDefinition: The field definition.

Return value

array Preloaded entries list array.

Throws

\Drupal\Component\Plugin\Exception\PluginNotFoundException

1 call to PreloadBuildTrait::buildPreLoaded()
MultiSelect2BoxesAutocompleteWidget::attachPreload in src/Plugin/Field/FieldWidget/MultiSelect2BoxesAutocompleteWidget.php
Attach preloaded data.

File

src/PreloadBuildTrait.php, line 27

Class

PreloadBuildTrait
Trait PreloadBuildTrait.

Namespace

Drupal\select2boxes

Code

protected function buildPreLoaded($count, FieldDefinitionInterface $fieldDefinition) {
  $entities = [];

  // Return empty array if the count is less than 1.
  if ($count <= 0 && $count != '') {
    return $entities;
  }

  /** @var \Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface $selection */
  $selection = \Drupal::service('plugin.manager.entity_reference_selection')
    ->getSelectionHandler($fieldDefinition);
  $referencable = $selection
    ->getReferenceableEntities(NULL, 'CONTAINS', $count);
  foreach ($referencable as $bundle) {
    foreach ($bundle as $id => $value) {
      $entities[$value] = [
        'id' => $id,
        'text' => $value,
      ];
    }
  }

  // Sort all bundles, then convert back to numeric based index for
  // javascript.
  ksort($entities);
  $entities = array_values($entities);
  return $entities;
}