public function FileExportFormatter::viewElements in REST Views 2.0.x
Same name and namespace in other branches
- 8 src/Plugin/Field/FieldFormatter/FileExportFormatter.php \Drupal\rest_views\Plugin\Field\FieldFormatter\FileExportFormatter::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
- src/
Plugin/ Field/ FieldFormatter/ FileExportFormatter.php, line 25
Class
- FileExportFormatter
- Plugin implementation of the 'file_export' formatter.
Namespace
Drupal\rest_views\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) : array {
$description = $this->fieldDefinition
->getSetting('description_field');
$elements = [];
/** @var \Drupal\Core\Field\EntityReferenceFieldItemListInterface $items */
foreach ($this
->getEntitiesToView($items, $langcode) as $delta => $entity) {
/** @var \Drupal\file\FileInterface $entity */
$data = [
'url' => file_create_url($entity
->getFileUri()),
];
if ($description && !empty($entity->_referringItem)) {
$data['description'] = $entity->_referringItem->description;
}
$elements[$delta] = [
'#type' => 'data',
'#data' => SerializedData::create($data),
];
}
return $elements;
}