You are here

public function EntityReferenceShsFormatter::viewElements in Simple hierarchical select 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/Field/FieldFormatter/EntityReferenceShsFormatter.php \Drupal\shs\Plugin\Field\FieldFormatter\EntityReferenceShsFormatter::viewElements()

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 EntityReferenceLabelFormatter::viewElements

File

src/Plugin/Field/FieldFormatter/EntityReferenceShsFormatter.php, line 52

Class

EntityReferenceShsFormatter
Plugin implementation of the 'entity reference taxonomy term SHS' formatter.

Namespace

Drupal\shs\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];
  $output_as_link = $this
    ->getSetting('link');
  foreach ($this
    ->getEntitiesToView($items, $langcode) as $delta => $entity) {

    // If the link is to be displayed and the entity has a uri, display a
    // link.
    if ($output_as_link && !$entity
      ->isNew()) {
      try {
        $entity
          ->toUrl();
      } catch (UndefinedLinkTemplateException $e) {

        // This exception is thrown by \Drupal\Core\Entity\Entity::urlInfo()
        // and it means that the entity type doesn't have a link template nor
        // a valid "uri_callback", so don't bother trying to output a link for
        // the rest of the referenced entities.
        $output_as_link = FALSE;
      }
    }
    try {
      $storage = \Drupal::entityTypeManager()
        ->getStorage($entity
        ->getEntityTypeId());
      $parents = $storage
        ->loadAllParents($entity
        ->id());
    } catch (Exception $ex) {
      $parents = [];
    }
    $list_items = [];

    // Create hierarchy from parent items.
    foreach (array_reverse($parents) as $entity_parent) {
      if ($entity_parent
        ->hasTranslation($langcode)) {
        $entity_parent = $entity_parent
          ->getTranslation($langcode);
      }
      if ($output_as_link) {
        $uri_parent = $entity_parent
          ->urlInfo();
        $list_items[] = [
          '#type' => 'link',
          '#title' => $entity_parent
            ->label(),
          '#url' => $uri_parent,
          '#options' => $uri_parent
            ->getOptions(),
        ];
      }
      else {
        $list_items[] = [
          '#plain_text' => $entity_parent
            ->label(),
        ];
      }
    }
    $elements[$delta] = [
      '#theme' => 'item_list',
      '#items' => $list_items,
      '#attributes' => [
        'class' => [
          'shs',
        ],
      ],
      '#attached' => [
        'library' => [
          'shs/shs.formatter',
        ],
      ],
    ];
    if (!empty($items[$delta]->_attributes)) {
      if (empty($elements[$delta]['#options'])) {
        $elements[$delta]['#options'] = [];
      }
      $elements[$delta]['#options'] += [
        'attributes' => [],
      ];
      $elements[$delta]['#options']['attributes'] += $items[$delta]->_attributes;

      // Unset field item attributes since they have been included in the
      // formatter output and shouldn't be rendered in the field template.
      unset($items[$delta]->_attributes);
    }
    if ($output_as_link) {
      $elements[$delta]['#attributes']['class'][] = 'shs-linked';
    }
    $elements[$delta]['#cache']['tags'] = $entity
      ->getCacheTags();
  }
  return $elements;
}