You are here

public function FaqFieldSimpleTextFormatter::viewElements in FAQ Field 8

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

Class

FaqFieldSimpleTextFormatter
Plugin implementation of the 'faqfield_simple_text' formatter.

Namespace

Drupal\faqfield\Plugin\Field\FieldFormatter

Code

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

    // Decide whether to use the default format or the custom one.
    $format = !empty($item->answer_format) ? $item->answer_format : $default_format;

    // Add each Q&A as page element,
    // to be rendered by the faqfield_simple_text_formatter template.
    $elements[$delta] = [
      '#theme' => 'faqfield_simple_text_formatter',
      '#question' => $item->question,
      '#answer' => $item->answer,
      '#answer_format' => $format,
      '#delta' => $delta,
    ];
  }
  return $elements;
}