protected function AutocompleteWidgetBase::getLabels

Gets the entity labels.

1 call to AutocompleteWidgetBase::getLabels()
AutocompleteWidgetBase::formElement in drupal/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidgetBase.php
Implements \Drupal\field\Plugin\Type\Widget\WidgetInterface::formElement().

File

drupal/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidgetBase.php, line 104
Contains \Drupal\entity_reference\Plugin\field\widget\AutocompleteWidgetBase.

Class

AutocompleteWidgetBase
Parent plugin for entity reference autocomplete widgets.

Namespace

Drupal\entity_reference\Plugin\field\widget

Code

protected function getLabels(array $items) {
  $entity_ids = array();
  $entity_labels = array();

  // Build an array of entity IDs.
  foreach ($items as $item) {
    $entity_ids[] = $item['target_id'];
  }

  // Load those entities and loop through them to extract their labels.
  $entities = entity_load_multiple($this->field['settings']['target_type'], $entity_ids);
  foreach ($entities as $entity_id => $entity_item) {
    $label = $entity_item
      ->label();
    $key = "{$label} ({$entity_id})";

    // Labels containing commas or quotes must be wrapped in quotes.
    if (strpos($key, ',') !== FALSE || strpos($key, '"') !== FALSE) {
      $key = '"' . str_replace('"', '""', $key) . '"';
    }
    $entity_labels[] = $key;
  }
  return $entity_labels;
}