public function EntityReferenceIdExportFormatter::viewElements in REST Views 2.0.x
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 EntityReferenceIdFormatter::viewElements
File
- src/
Plugin/ Field/ FieldFormatter/ EntityReferenceIdExportFormatter.php, line 34
Class
- EntityReferenceIdExportFormatter
- Plugin implementation of the 'entity reference ID' formatter.
Namespace
Drupal\rest_views\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) : array {
$elements = parent::viewElements($items, $langcode);
// Transform the plaintext elements into serialized data.
foreach ($elements as $delta => $element) {
// Cast the ID to an integer if it is a string containing only digits.
$id = ctype_digit($element['#plain_text']) ? (int) $element['#plain_text'] : $element['#plain_text'];
$elements[$delta]['#type'] = 'data';
$elements[$delta]['#data'] = SerializedData::create($id);
unset($elements[$delta]['#plain_text']);
}
return $elements;
}