You are here

function select2_publish_select2_autocomplete_matches_alter in Select 2 8

Implements hook_select2_autocomplete_matches_alter().

File

modules/select2_publish/select2_publish.module, line 23
This is the select2_publish module.

Code

function select2_publish_select2_autocomplete_matches_alter(array &$matches, array $options) {
  $entity_manager = \Drupal::entityTypeManager();
  $entity_definition = $entity_manager
    ->getDefinition($options['target_type']);
  if (!$entity_definition
    ->entityClassImplements(EntityPublishedInterface::class)) {
    return;
  }
  $entities = $entity_manager
    ->getStorage($options['target_type'])
    ->loadMultiple(array_keys($matches));

  /** @var \Drupal\Core\Entity\EntityPublishedInterface $entity */
  foreach ($entities as $id => $entity) {
    $matches[$id]['published'] = $entity
      ->isPublished();
  }
}