public function GdocFieldFormatter::viewElements in Embedded Google Docs Viewer 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 FormatterInterface::viewElements
File
- src/
Plugin/ Field/ FieldFormatter/ GdocFieldFormatter.php, line 53
Class
- GdocFieldFormatter
- Plugin implementation of the 'gdoc_field' formatter.
Namespace
Drupal\gdoc_field\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
foreach ($this
->getEntitiesToView($items, $langcode) as $delta => $file) {
$entity = $this->fieldDefinition
->getTargetEntityTypeId();
$bundle = $this->fieldDefinition
->getTargetBundle();
$field_name = $this->fieldDefinition
->getName();
$field_type = $this->fieldDefinition
->getType();
$file_uri = $file
->getFileUri();
$filename = $file
->getFileName();
$uri_scheme = StreamWrapperManager::getScheme($file_uri);
if ($uri_scheme == 'public') {
$url = file_create_url($file
->getFileUri());
$elements[$delta] = [
'#theme' => 'gdoc_field',
'#url' => $url,
'#filename' => $filename,
'#delta' => $delta,
'#entity' => $entity,
'#bundle' => $bundle,
'#field_name' => $field_name,
'#field_type' => $field_type,
'#attached' => [
'library' => [
'gdoc_field/gdoc-field',
],
],
];
}
else {
$this
->messenger()
->addError(t('The file (%file) is not publicly accessible. It must be publicly available in order for the Google Docs viewer to be able to access it.', [
'%file' => $filename,
]), FALSE);
}
}
return $elements;
}