Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. EntityAutocomplete.php

function 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.

6 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.
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.
NavigationLinkBlock::getUriAsDisplayableString in core/modules/navigation/src/Plugin/Block/NavigationLinkBlock.php
Gets the URI without the 'internal:' or 'entity:' scheme.

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

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal