public function OptionsDefaultFormatter::viewElements

Builds a renderable array for a field value.

Parameters

Drupal\Core\Entity\EntityInterface $entity: The entity being displayed.

string $langcode: The language associated with $items.

array $items: Array of values for this field.

Return value

array A renderable array for $items, as an array of child elements keyed by numeric indexes starting from 0.

Overrides FormatterInterface::viewElements

File

drupal/core/modules/options/lib/Drupal/options/Plugin/field/formatter/OptionsDefaultFormatter.php, line 35
Contains \Drupal\options\Plugin\field\formatter\OptionsDefaultFormatter.

Class

OptionsDefaultFormatter
Plugin implementation of the 'list_default' formatter.

Namespace

Drupal\options\Plugin\field\formatter

Code

public function viewElements(EntityInterface $entity, $langcode, array $items) {
  $elements = array();
  $allowed_values = options_allowed_values($this->field, $this->instance, $entity);
  foreach ($items as $delta => $item) {
    if (isset($allowed_values[$item['value']])) {
      $output = field_filter_xss($allowed_values[$item['value']]);
    }
    else {

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