View source
<?php
namespace Drupal\entity_print;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\entity_print\Event\PrintEvents;
use Drupal\entity_print\Event\PreSendPrintEvent;
use Drupal\entity_print\Plugin\PrintEngineInterface;
use Drupal\entity_print\Renderer\RendererFactoryInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class PrintBuilder implements PrintBuilderInterface {
use StringTranslationTrait;
protected $rendererFactory;
protected $dispatcher;
public function __construct(RendererFactoryInterface $renderer_factory, EventDispatcherInterface $event_dispatcher, TranslationInterface $string_translation) {
$this->rendererFactory = $renderer_factory;
$this->dispatcher = $event_dispatcher;
$this->stringTranslation = $string_translation;
}
public function deliverPrintable(array $entities, PrintEngineInterface $print_engine, $force_download = FALSE, $use_default_css = TRUE) {
$renderer = $this
->prepareRenderer($entities, $print_engine, $use_default_css);
$this->dispatcher
->dispatch(PrintEvents::PRE_SEND, new PreSendPrintEvent($print_engine, $entities));
$filename = $renderer
->getFilename($entities) . '.' . $print_engine
->getExportType()
->getFileExtension();
return $print_engine
->send($filename, $force_download);
}
public function printHtml(EntityInterface $entity, $use_default_css = TRUE, $optimize_css = TRUE) {
$renderer = $this->rendererFactory
->create([
$entity,
]);
$content[] = $renderer
->render([
$entity,
]);
$render = [
'#theme' => 'entity_print__' . $entity
->getEntityTypeId() . '__' . $entity
->bundle(),
'#title' => $this
->t('View'),
'#content' => $content,
'#attached' => [],
];
return $renderer
->generateHtml([
$entity,
], $render, $use_default_css, $optimize_css);
}
public function savePrintable(array $entities, PrintEngineInterface $print_engine, $scheme = 'public', $filename = FALSE, $use_default_css = TRUE) {
$renderer = $this
->prepareRenderer($entities, $print_engine, $use_default_css);
$this->dispatcher
->dispatch(PrintEvents::PRE_SEND, new PreSendPrintEvent($print_engine, $entities));
if (!$filename) {
$filename = $renderer
->getFilename($entities) . '.' . $print_engine
->getExportType()
->getFileExtension();
}
$uri = "{$scheme}://{$filename}";
return \Drupal::service('file_system')
->saveData($print_engine
->getBlob(), $uri, FileSystemInterface::EXISTS_REPLACE);
}
protected function prepareRenderer(array $entities, PrintEngineInterface $print_engine, $use_default_css) {
if (empty($entities)) {
throw new \InvalidArgumentException('You must pass at least 1 entity');
}
$renderer = $this->rendererFactory
->create($entities);
$content = $renderer
->render($entities);
$first_entity = reset($entities);
$render = [
'#theme' => 'entity_print__' . $first_entity
->getEntityTypeId() . '__' . $first_entity
->bundle(),
'#title' => $this
->t('View @type', [
'@type' => $print_engine
->getExportType()
->label(),
]),
'#content' => $content,
'#attached' => [],
];
$print_engine
->addPage($renderer
->generateHtml($entities, $render, $use_default_css, TRUE));
return $renderer;
}
}