public function KeyValueFormatter::viewElements in Key value field 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 TextDefaultFormatter::viewElements
File
- src/
Plugin/ Field/ FieldFormatter/ KeyValueFormatter.php, line 69
Class
- KeyValueFormatter
- Plugin implementation of the 'key_value' formatter.
Namespace
Drupal\key_value_field\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
// Get the value elements from the TextDefaultFormatter class.
$value_elements = parent::viewElements($items, $langcode);
// Buffer the return value.
$elements = [];
// Loop through all items.
foreach ($items as $delta => $item) {
// Just add the key element to the render array, when 'value_only' is not
// checked.s
if (!$this
->getSetting('value_only')) {
$elements[$delta]['key'] = [
'#plain_text' => nl2br($item->key . ' : '),
];
}
// Add the value to the render array.
$elements[$delta]['value'] = $value_elements[$delta];
}
return $elements;
}