You are here

public function FaqFieldDetailsFormatter::viewElements in FAQ Field 8

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

Class

FaqFieldDetailsFormatter
Plugin implementation of the 'faqfield_details' 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;
    $elements[$delta] = [
      '#theme' => 'faqfield_details_formatter',
      '#question' => $item->question,
      '#answer' => $item->answer,
      '#answer_format' => $format,
      '#delta' => $delta,
    ];
  }
  return $elements;
}