You are here

public function StrawSelection::getReferenceableEntities in Super Term Reference Autocomplete Widget 8

Gets the list of referenceable entities.

Return value

array A nested array of entities, the first level is keyed by the entity bundle, which contains an array of entity labels (escaped), keyed by the entity ID.

Overrides TermSelection::getReferenceableEntities

1 call to StrawSelection::getReferenceableEntities()
StrawSelection::createNewEntity in src/Plugin/EntityReferenceSelection/StrawSelection.php
Creates a new entity object that can be used as a valid reference.

File

src/Plugin/EntityReferenceSelection/StrawSelection.php, line 27

Class

StrawSelection
Provides specific access control for the taxonomy_term entity type.

Namespace

Drupal\straw\Plugin\EntityReferenceSelection

Code

public function getReferenceableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
  $options = [];
  $bundles = $this->entityTypeBundleInfo
    ->getBundleInfo('taxonomy_term');
  $handler_settings = $this->configuration['handler_settings'];
  $bundle_names = !empty($handler_settings['target_bundles']) ? $handler_settings['target_bundles'] : array_keys($bundles);
  foreach ($bundle_names as $bundle) {
    if ($vocabulary = Vocabulary::load($bundle)) {
      foreach ($this
        ->getSelectableTerms($vocabulary
        ->id()) as $term) {
        $referenceable = TRUE;
        if ($match && $match_operator == "CONTAINS") {
          $referenceable = stripos($term['tree_path'], $match) !== FALSE;
        }
        if ($match && $match_operator == "STARTS_WITH") {
          $referenceable = stripos($term['tree_path'], $match) === 0;
        }
        if ($match && $match_operator == "=") {
          $referenceable = $term['tree_path'] == $match;
        }
        if ($referenceable) {
          $options[$vocabulary
            ->id()][$term['tid']] = Html::escape($term['tree_path']);
          if ($limit > 0 && count($options[$vocabulary
            ->id()]) == $limit) {
            break;
          }
        }
      }
    }
  }
  return $options;
}