You are here

public function FaqFieldAnchorListFormatter::viewElements in FAQ Field 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/Field/FieldFormatter/FaqFieldAnchorListFormatter.php \Drupal\faqfield\Plugin\Field\FieldFormatter\FaqFieldAnchorListFormatter::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 FormatterInterface::viewElements

File

src/Plugin/Field/FieldFormatter/FaqFieldAnchorListFormatter.php, line 68

Class

FaqFieldAnchorListFormatter
Plugin implementation of the 'faqfield_anchor_list' formatter.

Namespace

Drupal\faqfield\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $default_format = $this
    ->getFieldSetting('default_format');
  $element_items = [];
  foreach ($items as $item) {

    // Decide whether to use the default format or the custom one.
    $format = !empty($item->answer_format) ? $item->answer_format : $default_format;
    $element_items[] = [
      'question' => $item->question,
      'answer' => $item->answer,
      'answer_format' => $format,
    ];
  }
  $elements = [];
  if ($element_items) {
    $elements[0] = [
      '#theme' => 'faqfield_anchor_list_formatter',
      '#items' => $element_items,
      '#list_type' => $this
        ->getSetting('anchor_list_type'),
    ];
  }
  return $elements;
}