You are here

public function EpubReaderJs::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/EpubReaderJs.php, line 29
Contains \Drupal\epub\Plugin\Field\FieldFormatter\EpubReaderJs.

Class

EpubReaderJs
Plugin implementation of the 'epub' formatter.

Namespace

Drupal\epub\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  global $base_url;
  $elements = array();
  foreach ($this
    ->getEntitiesToView($items, $langcode) as $delta => $file) {
    if ($file
      ->getMimeType() == 'application/epub+zip') {
      $file_url = file_create_url($file
        ->getFileUri());
      $reader = $base_url . '/libraries/epub.js/reader/index.html';
      $elements[$delta] = array(
        '#theme' => 'epub_formatter_reader',
        '#file' => $reader . '?' . $file_url,
      );
    }
    else {
      $elements[$delta] = array(
        '#theme' => 'file_link',
        '#file' => $file,
      );
    }
  }
  return $elements;
}