You are here

trait PreloadBuildTrait in Select2 Boxes 8

Trait PreloadBuildTrait.

@package Drupal\select2boxes

Hierarchy

1 file declares its use of PreloadBuildTrait
MultiSelect2BoxesAutocompleteWidget.php in src/Plugin/Field/FieldWidget/MultiSelect2BoxesAutocompleteWidget.php

File

src/PreloadBuildTrait.php, line 12

Namespace

Drupal\select2boxes
View source
trait PreloadBuildTrait {

  /**
   * Build preloaded entries list.
   *
   * @param string $count
   *   Number of entries will be preloaded, or empty string to load all.
   * @param \Drupal\Core\Field\FieldDefinitionInterface $fieldDefinition
   *   The field definition.
   *
   * @return array
   *   Preloaded entries list array.
   *
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PreloadBuildTrait::buildPreLoaded protected function Build preloaded entries list.