You are here

public function ExtraFieldPlusDisplayFormattedBase::view in Extra Field Settings Provider 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/ExtraFieldPlusDisplayFormattedBase.php \Drupal\extra_field_plus\Plugin\ExtraFieldPlusDisplayFormattedBase::view()

File

src/Plugin/ExtraFieldPlusDisplayFormattedBase.php, line 32

Class

ExtraFieldPlusDisplayFormattedBase
Base class for Extra Field Plus Display plugins with field wrapper output.

Namespace

Drupal\extra_field_plus\Plugin

Code

public function view(ContentEntityInterface $entity) {
  $elements = $this
    ->viewElements($entity);
  if (!empty($elements) && !$this
    ->isEmpty()) {

    // Construct a render array for the extra field elements.
    // @see \Drupal\Core\Field\FormatterBase::view
    $build = [
      '#theme' => 'field',
      '#title' => $this
        ->getLabel(),
      '#label_display' => $this
        ->getLabelDisplay(),
      '#view_mode' => '_custom',
      '#language' => $this
        ->getLangcode(),
      '#field_name' => $this
        ->getFieldName(),
      '#field_type' => $this
        ->getFieldType(),
      '#field_translatable' => $this
        ->isTranslatable(),
      '#entity_type' => $entity
        ->getEntityTypeId(),
      '#bundle' => $entity
        ->bundle(),
      '#object' => $entity,
      '#formatter' => $this
        ->getPluginId(),
    ];
    if ($children = Element::children($elements, TRUE)) {
      $build['#is_multiple'] = TRUE;

      // Without #children the field will not show up.
      $build['#children'] = '';
      foreach ($children as $key) {

        // Only keys in "#items" property are required in
        // template_preprocess_field().
        $build['#items'][$key] = new \stdClass();
        $build[$key] = $elements[$key];
      }
    }
    else {
      $build['#is_multiple'] = FALSE;

      // Only keys in "#items" property are required in
      // template_preprocess_field().
      $build['#items'][] = new \stdClass();
      $build[] = $elements;
    }
  }
  else {
    $build = $elements;
  }
  return $build;
}