function EntityAutocomplete::getEntityLabels

Same name in other branches
  1. 9 core/lib/Drupal/Core/Entity/Element/EntityAutocomplete.php \Drupal\Core\Entity\Element\EntityAutocomplete::getEntityLabels()
  2. 8.9.x core/lib/Drupal/Core/Entity/Element/EntityAutocomplete.php \Drupal\Core\Entity\Element\EntityAutocomplete::getEntityLabels()
  3. 10 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.

7 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 format needed by the EntityAutocomplete element.
EntityReference::valueFormAddAutocomplete in core/modules/views/src/Plugin/views/filter/EntityReference.php
Adds an autocomplete element to the form.
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.

... See full list

File

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

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);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.