You are here

public static function EntityAutocomplete::getEntityLabels in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Entity/Element/EntityAutocomplete.php \Drupal\Core\Entity\Element\EntityAutocomplete::getEntityLabels()

Converts an array of entity objects into a string of entity labels.

This method is also responsible for checking the 'view label' access on the passed-in entities.

Parameters

\Drupal\Core\Entity\EntityInterface[] $entities: An array of entity objects.

Return value

string A string of entity labels separated by commas.

5 calls to EntityAutocomplete::getEntityLabels()
EntityAutocomplete::valueCallback in core/lib/Drupal/Core/Entity/Element/EntityAutocomplete.php
Determines how user input is mapped to an element's #value property.
EntityAutocompleteElementFormTest::getAutocompleteInput in core/tests/Drupal/KernelTests/Core/Entity/Element/EntityAutocompleteElementFormTest.php
Returns an entity label in the format needed by the EntityAutocomplete element.
LinkWidget::getUriAsDisplayableString in core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php
Gets the URI without the 'internal:' or 'entity:' scheme.
Name::valueForm in core/modules/user/src/Plugin/views/filter/Name.php
Options form subform for setting options.
TaxonomyIndexTid::valueForm in core/modules/taxonomy/src/Plugin/views/filter/TaxonomyIndexTid.php
Options form subform for setting options.

File

core/lib/Drupal/Core/Entity/Element/EntityAutocomplete.php, line 365

Class

EntityAutocomplete
Provides an entity autocomplete form element.

Namespace

Drupal\Core\Entity\Element

Code

public static function getEntityLabels(array $entities) {

  /** @var \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository */
  $entity_repository = \Drupal::service('entity.repository');
  $entity_labels = [];
  foreach ($entities as $entity) {

    // Set the entity in the correct language for display.
    $entity = $entity_repository
      ->getTranslationFromContext($entity);

    // Use the special view label, since some entities allow the label to be
    // viewed, even if the entity is not allowed to be viewed.
    $label = $entity
      ->access('view label') ? $entity
      ->label() : t('- Restricted access -');

    // Take into account "autocreated" entities.
    if (!$entity
      ->isNew()) {
      $label .= ' (' . $entity
        ->id() . ')';
    }

    // Labels containing commas or quotes must be wrapped in quotes.
    $entity_labels[] = Tags::encode($label);
  }
  return implode(', ', $entity_labels);
}