You are here

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

Class

Epubjs
Plugin implementation of the 'epubJs' 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) {
    $file_url = file_create_url($file
      ->getFileUri());
    if ($file
      ->getMimeType() == 'application/epub+zip') {
      if ($this
        ->getSetting('epub_unzipped')) {
        $path = file_create_url('public://epub_content/' . $file
          ->id());
        $epub = $base_url . '/libraries/epub.js/reader/index.html?bookPath=' . $path . '/';
      }
      else {
        $epub = $base_url . '/libraries/epub.js/reader/index.html?bookPath=' . $file_url;
      }
      $file_url = file_create_url($file
        ->getFileUri());
      $elements[$delta] = array(
        '#theme' => 'epub_formatter_js',
        '#file' => $file,
        '#reader' => $epub,
        '#width' => $this
          ->getSetting('width'),
        '#height' => $this
          ->getSetting('height'),
      );
    }
    else {
      $elements[$delta] = array(
        '#theme' => 'file_link',
        '#file' => $file,
      );
    }
  }
  return $elements;
}