public function OptionsDefaultFormatter::viewElements in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/options/src/Plugin/Field/FieldFormatter/OptionsDefaultFormatter.php \Drupal\options\Plugin\Field\FieldFormatter\OptionsDefaultFormatter::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
- core/
modules/ options/ src/ Plugin/ Field/ FieldFormatter/ OptionsDefaultFormatter.php, line 36 - Contains \Drupal\options\Plugin\Field\FieldFormatter\OptionsDefaultFormatter.
Class
- OptionsDefaultFormatter
- Plugin implementation of the 'list_default' formatter.
Namespace
Drupal\options\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
// 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 = isset($options[$value]) ? $options[$value] : $value;
$elements[$delta] = array(
'#markup' => $output,
'#allowed_tags' => FieldFilteredMarkup::allowedTags(),
);
}
}
return $elements;
}