public function EpubCoverImage::viewElements in Epub 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/ EpubCoverImage.php, line 31 - Contains \Drupal\epub\Plugin\Field\FieldFormatter\EpubCoverImage.
Class
- EpubCoverImage
- Plugin implementation of the 'epub' formatter.
Namespace
Drupal\epub\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
foreach ($this
->getEntitiesToView($items, $langcode) as $delta => $file) {
$file_url = file_create_url($file
->getFileUri());
if ($file
->getMimeType() == 'application/epub+zip') {
$elements[$delta] = array(
'#theme' => 'epub_formatter_cover',
'#file' => $file_url,
'#width' => $this
->getSetting('width'),
);
$dir = 'public://epub_content/' . $file
->id();
$scan = file_scan_directory($dir, '/.*\\.opf$/');
$opf = array_shift($scan);
if (isset($opf)) {
$opfXML = simplexml_load_file($opf->uri);
$opfXML
->registerXPathNamespace('opf', 'http://www.idpf.org/2007/opf');
$element = $opfXML
->xpath('/opf:package/opf:metadata/opf:meta[@name="cover"]');
if (count($element)) {
$attributes = $element[0]
->attributes();
$elements[$delta]['#image'] = file_create_url(epub_get_item($dir, (string) $attributes['content']));
}
}
}
else {
$elements[$delta] = array(
'#theme' => 'file_link',
'#file' => $file,
);
}
}
return $elements;
}