You are here

public function SelectOtherFormatter::viewElements in CCK Select Other 8

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/SelectOtherFormatter.php, line 27

Class

SelectOtherFormatter
Plugin implementation of the 'cck_select_other' formatter.

Namespace

Drupal\cck_select_other\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];
  $allowed_values = $this
    ->getFieldSetting('allowed_values');
  foreach ($items as $delta => $item) {
    if (isset($allowed_values[$item->value])) {
      $output = FieldFilteredMarkup::create($allowed_values[$item->value]);
    }
    else {

      // If no match was found in allowed values, fall back to the key.
      $output = FieldFilteredMarkup::create($item->value);
    }
    $elements[$delta] = [
      '#markup' => $output,
    ];
  }
  return $elements;
}