OptionsDefaultFormatter.php

Same filename in other branches
  1. 9 core/modules/options/src/Plugin/Field/FieldFormatter/OptionsDefaultFormatter.php
  2. 8.9.x core/modules/options/src/Plugin/Field/FieldFormatter/OptionsDefaultFormatter.php
  3. 11.x core/modules/options/src/Plugin/Field/FieldFormatter/OptionsDefaultFormatter.php

Namespace

Drupal\options\Plugin\Field\FieldFormatter

File

core/modules/options/src/Plugin/Field/FieldFormatter/OptionsDefaultFormatter.php

View source
<?php

namespace Drupal\options\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\Attribute\FieldFormatter;
use Drupal\Core\Field\FieldFilteredMarkup;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\OptGroup;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Plugin implementation of the 'list_default' formatter.
 */
class OptionsDefaultFormatter extends FormatterBase {
    
    /**
     * {@inheritdoc}
     */
    public function viewElements(FieldItemListInterface $items, $langcode) {
        $elements = [];
        // Only collect allowed options if there are actually items to display.
        if ($items->count()) {
            $provider = $items->getFieldDefinition()
                ->getFieldStorageDefinition()
                ->getOptionsProvider('value', $items->getEntity());
            // Flatten the possible options, to support opt groups.
            $options = OptGroup::flattenOptions($provider->getPossibleOptions());
            foreach ($items as $delta => $item) {
                $value = $item->value;
                // If the stored value is in the current set of allowed values, display
                // the associated label, otherwise just display the raw value.
                $output = $options[$value] ?? $value;
                $elements[$delta] = [
                    '#markup' => $output,
                    '#allowed_tags' => FieldFilteredMarkup::allowedTags(),
                ];
            }
        }
        return $elements;
    }

}

Classes

Title Deprecated Summary
OptionsDefaultFormatter Plugin implementation of the 'list_default' formatter.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.