epub_viewer-8.x-1.0/src/Controller/EpubController.php
src/Controller/EpubController.php
<?php
namespace Drupal\epub_module\Controller;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Extension\ModuleExtensionList;
use Drupal\Core\File\FileUrlGeneratorInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Response;
/**
* Controller for rendering epub file to the ebook viewer.
*/
class EpubController extends ControllerBase {
/**
* Returns the file_url_generator service.
*
* @var \Drupal\Core\File\FileUrlGeneratorInterface
*/
protected $fileUrl;
/**
* Returns the extension.list.module service.
*
* @var \Drupal\Core\Extension\ModuleExtensionList
*/
protected $listModule;
/**
* Returns the renderer service.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* Returns the config.factory service.
*
* @var \Drupal\Core\Config\ConfigFactory
*/
protected $config;
/**
* Constructs a EpubController object.
*
* @param \Drupal\Core\File\FileUrlGeneratorInterface $file_url
* Generates file URLs for a stream to an external or local file.
* @param \Drupal\Core\Extension\ModuleExtensionList $list_module
* GProvides a list of available modules.
* @param \Drupal\Core\Render\RendererInterface $renderer
* Defines an interface for turning a render array into a string.
* @param \Drupal\Core\Config\ConfigFactory $config
* Defines the configuration object factory.
*/
public function __construct(FileUrlGeneratorInterface $file_url, ModuleExtensionList $list_module, RendererInterface $renderer, ConfigFactory $config) {
$this->fileUrl = $file_url;
$this->listModule = $list_module;
$this->renderer = $renderer;
$this->config = $config;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('file_url_generator'),
$container->get('extension.list.module'),
$container->get('renderer'),
$container->get('config.factory')
);
}
/**
* Function for rendering epub file to the ebook viewer.
*/
public function viewEbook($fid = FALSE) {
if ($fid) {
global $base_url;
$data = [];
// Loading file from file ID.
$file = $this->entityTypeManager()->getStorage('file')->load($fid);
$file_uri = $file->getFileUri();
$file_url = Url::fromUri($this->fileUrl->generateAbsoluteString($file_uri));
$data['file_path'] = $file_url->getUri();
$data['base_url'] = $base_url;
$config = $this->config->get('epub_module.epubsettings');
$options = [
'background_color' => $config->get('background_color'),
'icon_color' => $config->get('icon_color'),
'font_color' => $config->get('font_color'),
'show_download_icon' => $config->get('show_download_icon'),
];
$module_path = $this->listModule->getPath('epub_module');
$data['module_path'] = $base_url . '/' . $module_path;
$build = [
'#theme' => 'epub_view',
// Sending file data to the viewer.
'#data' => $data,
'#options' => $options,
];
$html = $this->renderer->renderRoot($build);
$response = new Response();
$response->setContent($html);
return $response;
}
}
}
