You are here

public function HierarchicalFormatter::viewElements in Hierarchical Term Formatter 8

Builds a renderable array for a field value.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.

string $langcode: The language that should be used to render the field.

Return value

array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.

Overrides FormatterInterface::viewElements

File

src/Plugin/Field/FieldFormatter/HierarchicalFormatter.php, line 200

Class

HierarchicalFormatter
Plugin implementation of the 'rating' formatter.

Namespace

Drupal\hierarchical_term_formatter\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = $used = [];
  foreach ($items as $delta => $item) {
    $item_value = $item
      ->getValue();
    $tid = $item_value['target_id'];
    $term_tree = [];
    switch ($this
      ->getSetting('display')) {
      case 'leaf':
        $term = $this->taxonomyTermStorage
          ->load($tid);
        $term_tree = [
          $term,
        ];
        break;
      case 'root':
        $parents = $this->taxonomyTermStorage
          ->loadAllParents($tid);
        if (!empty($parents)) {
          $term_tree = [
            array_pop($parents),
          ];
          if (isset($used[$term_tree[0]
            ->id()])) {
            $term_tree = [];
            break;
          }
          $used[$term_tree[0]
            ->id()] = TRUE;
        }
        break;
      case 'parents':
        $term_tree = array_reverse($this->taxonomyTermStorage
          ->loadAllParents($tid));
        array_pop($term_tree);
        break;
      case 'nonroot':
        $parents = $this->taxonomyTermStorage
          ->loadAllParents($tid);
        if (count($parents) > 1) {
          $term_tree = array_reverse($parents);

          // This gets rid of the first topmost term.
          array_shift($term_tree);

          // Terms can have multiple parents. Now remove any remaining topmost
          // terms.
          foreach ($term_tree as $key => $term) {
            $has_parents = $this->taxonomyTermStorage
              ->loadAllParents($term
              ->id());

            // This has no parents and is topmost.
            if (empty($has_parents)) {
              unset($term_tree[$key]);
            }
          }
        }
        break;
      default:
        $term_tree = array_reverse($this->taxonomyTermStorage
          ->loadAllParents($tid));
        break;
    }

    // Change output order if Reverse order is checked.
    if ($this
      ->getSetting('reverse') && count($term_tree)) {
      $term_tree = array_reverse($term_tree);
    }

    // Remove empty elements caused by discarded items.
    $term_tree = array_filter($term_tree);
    if (!$term_tree) {
      break;
    }
    foreach ($term_tree as $index => $term) {
      if ($term
        ->hasTranslation($langcode)) {
        $term_tree[$index] = $term
          ->getTranslation($langcode);
      }
    }
    $elements[$delta] = [
      '#theme' => 'hierarchical_term_formatter',
      '#terms' => $term_tree,
      '#wrapper' => $this
        ->getSetting('wrap'),
      '#separator' => $this
        ->getSetting('separator'),
      '#link' => $this
        ->getSetting('link') ? TRUE : FALSE,
    ];
  }
  return $elements;
}