You are here

public static function SuperTermReferenceAutocomplete::getEntityLabels in Super Term Reference Autocomplete Widget 8

Gets the term label with its hierarchy.

Converts an array of term objects into a string of term labels including the full tree path to the term. This method is also responsible for checking the 'view label' access on the passed-in terms.

Parameters

\Drupal\taxonomy\Entity\Term[] $terms: An array of term objects.

Return value

string A string of term labels separated by commas.

Overrides EntityAutocomplete::getEntityLabels

File

src/Element/SuperTermReferenceAutocomplete.php, line 31

Class

SuperTermReferenceAutocomplete
Provides an entity autocomplete form element.

Namespace

Drupal\straw\Element

Code

public static function getEntityLabels(array $terms) {
  $term_labels = [];

  /** @var \Drupal\taxonomy\TermStorage $term_storage */
  $term_storage = \Drupal::entityTypeManager()
    ->getStorage('taxonomy_term');
  foreach ($terms as $term) {

    // 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 = t('- Restricted access -');
    if ($term
      ->access('view label')) {

      // For Straw widgets, we want to show the full tree path to the current
      // term rather than just the current term's label.
      $label = $term
        ->label();
      $current = $term;
      while ($parents = $term_storage
        ->loadParents($current
        ->id())) {
        $parent = reset($parents);
        $label = $parent
          ->label() . ' >> ' . $label;
        $current = $parent;
      }
    }

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

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