You are here

public function FaqFieldDefinitionListFormatter::viewElements in FAQ Field 8

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

Class

FaqFieldDefinitionListFormatter
Plugin implementation of the 'faqfield_definition_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_definition_list_formatter',
      '#items' => $element_items,
    ];
  }
  return $elements;
}