You are here

protected function AutocompleteWidgetBase::getLabels in Entity reference 8

Get the entity labels.

1 call to AutocompleteWidgetBase::getLabels()
AutocompleteWidgetBase::prepareElement in lib/Drupal/entityreference/Plugin/field/widget/AutocompleteWidgetBase.php
Prepapre the element.

File

lib/Drupal/entityreference/Plugin/field/widget/AutocompleteWidgetBase.php, line 108
Definition of Drupal\entityreference\Plugin\field\widget\AutocompleteWidgetBase.

Class

AutocompleteWidgetBase
Parent plugin for entity-reference autocomplete widgets.

Namespace

Drupal\entityreference\Plugin\field\widget

Code

protected function getLabels(array $items) {
  $instance = $this->instance;
  $field = $this->field;
  $entity = isset($element['#entity']) ? $element['#entity'] : NULL;
  $handler = entityreference_get_selection_handler($field, $instance, $entity);
  $entity_ids = array();
  $entity_labels = array();

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

  // Load those entities and loop through them to extract their labels.
  $entities = entity_load_multiple($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;
}