diff --git a/config/install/entity_print.settings.yml b/config/install/entity_print.settings.yml index 68ac2ca..4b47d63 100644 --- a/config/install/entity_print.settings.yml +++ b/config/install/entity_print.settings.yml @@ -1,3 +1,4 @@ default_css: true force_download: true -pdf_engine: dompdf +print_engines: + pdf_engine: dompdf diff --git a/config/install/system.action.entity_print_download_action.yml b/config/install/system.action.entity_print_download_action.yml deleted file mode 100644 index 99a1291..0000000 --- a/config/install/system.action.entity_print_download_action.yml +++ /dev/null @@ -1,10 +0,0 @@ -langcode: en -status: true -dependencies: - module: - - entity_print -id: entity_print_download_action -label: 'Download PDF' -type: node -plugin: entity_print_download_action -configuration: { } diff --git a/config/install/system.action.entity_print_pdf_download_action.yml b/config/install/system.action.entity_print_pdf_download_action.yml new file mode 100644 index 0000000..13e10dc --- /dev/null +++ b/config/install/system.action.entity_print_pdf_download_action.yml @@ -0,0 +1,10 @@ +langcode: en +status: true +dependencies: + module: + - entity_print +id: entity_print_pdf_download_action +label: 'Download PDF' +type: node +plugin: entity_print_pdf_download_action +configuration: { } diff --git a/config/schema/entity_print.schema.yml b/config/schema/entity_print.schema.yml index 7a6b926..b1e5982 100644 --- a/config/schema/entity_print.schema.yml +++ b/config/schema/entity_print.schema.yml @@ -7,23 +7,27 @@ entity_print.settings: label: 'Enable the default CSS.' force_download: type: boolean - label: 'Force download of PDF' - pdf_engine: - type: string - label: 'The plugin id of the PDF engine to use.' + label: 'Force download of Print' + print_engines: + type: mapping + label: 'The plugin id of the Print engine to use.' + mapping: + pdf_engine: + type: string + label: 'The plugin id of the PDF engine to use.' -entity_print.pdf_engine.*: +entity_print.print_engine.*: type: config_entity - label: 'PDF Engine' + label: 'Print Engine' mapping: id: type: string - label: 'PDF Engine Id' + label: 'Print Engine Id' settings: - type: entity_print_pdf_engine.[%parent.id] - label: 'Settings for the PDF Engine.' + type: entity_print_print_engine.[%parent.id] + label: 'Settings for the Print Engine.' -entity_print_pdf_engine.dompdf: +entity_print_print_engine.dompdf: type: mapping label: 'Dompdf specific settings' mapping: @@ -37,7 +41,7 @@ entity_print_pdf_engine.dompdf: type: boolean label: 'Enable remote urls' -entity_print_pdf_engine.phpwkhtmltopdf: +entity_print_print_engine.phpwkhtmltopdf: type: mapping label: 'PHP Wkhtmltopdf' type: mapping @@ -55,6 +59,6 @@ core.entity_view_display.*.*.*.third_party.entity_print: type: string label: 'Label for view link' -action.configuration.entity_print_download_action: +action.configuration.entity_print_pdf_download_action: type: action_configuration_default label: 'Download entities as a PDF' diff --git a/entity_print.routing.yml b/entity_print.routing.yml index 0101866..1d98bc7 100644 --- a/entity_print.routing.yml +++ b/entity_print.routing.yml @@ -1,7 +1,7 @@ entity_print.view: path: 'entityprint/{entity_type}/{entity_id}' defaults: - _controller: '\Drupal\entity_print\Controller\EntityPrintController::viewPdf' + _controller: '\Drupal\entity_print\Controller\EntityPrintController::viewPrint' _title: 'Entity Print' requirements: _permission: 'entity print access' @@ -10,7 +10,7 @@ entity_print.view: entity_print.view.debug: path: 'entityprint/{entity_type}/{entity_id}/debug' defaults: - _controller: '\Drupal\entity_print\Controller\EntityPrintController::viewPdfDebug' + _controller: '\Drupal\entity_print\Controller\EntityPrintController::viewPrintDebug' _title: 'Entity Print Debug' requirements: _permission: 'entity print access' diff --git a/entity_print.services.yml b/entity_print.services.yml index 6747f70..a35597a 100644 --- a/entity_print.services.yml +++ b/entity_print.services.yml @@ -1,9 +1,9 @@ services: - plugin.manager.entity_print.pdf_engine: + plugin.manager.entity_print.print_engine: class: Drupal\entity_print\Plugin\EntityPrintPluginManager arguments: ['@container.namespaces', '@cache.discovery', '@module_handler', '@event_dispatcher'] entity_print.print_manager: - class: Drupal\entity_print\EntityPrintPrintBuilder + class: Drupal\entity_print\PrintBuilder arguments: ['@entity_print.renderer_factory', '@event_dispatcher'] entity_print.renderer_factory: class: Drupal\entity_print\Renderer\RendererFactory diff --git a/src/Annotation/PdfEngine.php b/src/Annotation/PdfEngine.php deleted file mode 100644 index 3b74537..0000000 --- a/src/Annotation/PdfEngine.php +++ /dev/null @@ -1,33 +0,0 @@ -get('plugin.manager.entity_print.pdf_engine'), + $container->get('plugin.manager.entity_print.print_engine'), $container->get('entity_print.print_manager'), $container->get('entity_type.manager') ); } /** - * Output an entity as a PDF. + * Output an entity as a Print. * * @param string $entity_type * The entity type. @@ -72,32 +72,32 @@ class EntityPrintController extends ControllerBase { * The entity id. * * @return \Symfony\Component\HttpFoundation\Response - * The response object on error otherwise the PDF is sent. + * The response object on error otherwise the Print is sent. */ - public function viewPdf($entity_type, $entity_id) { - // Create the PDF engine plugin. + public function viewPrint($entity_type, $entity_id) { + // Create the Print engine plugin. $config = $this->config('entity_print.settings'); $entity = $this->entityTypeManager->getStorage($entity_type)->load($entity_id); try { - $pdf_engine = $this->pluginManager->createInstance($config->get('pdf_engine')); + $print_engine = $this->pluginManager->createInstance($config->get('print_engines')); } - catch (PdfEngineException $e) { + catch (PrintEngineException $e) { // Build a safe markup string using Xss::filter() so that the instructions // for installing dependencies can contain quotes. - drupal_set_message(new FormattableMarkup('Error generating PDF: ' . Xss::filter($e->getMessage()), []), 'error'); + drupal_set_message(new FormattableMarkup('Error generating Print: ' . Xss::filter($e->getMessage()), []), 'error'); return new RedirectResponse($entity->toUrl()->toString()); } - return (new StreamedResponse(function() use ($entity, $pdf_engine, $config) { - // The PDF is sent straight to the browser. - $this->printBuilder->getEntityRenderedAsPrint($entity, $pdf_engine, $config->get('force_download'), $config->get('default_css')); + return (new StreamedResponse(function() use ($entity, $print_engine, $config) { + // The Print is sent straight to the browser. + $this->printBuilder->printSingle($entity, $print_engine, $config->get('force_download'), $config->get('default_css')); }))->send(); } /** - * A debug callback for styling up the PDF. + * A debug callback for styling up the Print. * * @param string $entity_type * The entity type. @@ -107,14 +107,14 @@ class EntityPrintController extends ControllerBase { * @return \Symfony\Component\HttpFoundation\Response * The response object. */ - public function viewPdfDebug($entity_type, $entity_id) { + public function viewPrintDebug($entity_type, $entity_id) { $entity = $this->entityTypeManager->getStorage($entity_type)->load($entity_id); try { $use_default_css = $this->config('entity_print.settings')->get('default_css'); - return new Response($this->pdfBuilder->getEntityRenderedAsHtml($entity, $use_default_css, $this->config('system.performance')->get('css.preprocess'))); + return new Response($this->printBuilder->printHtml($entity, $use_default_css, $this->config('system.performance')->get('css.preprocess'))); } - catch (PdfEngineException $e) { - drupal_set_message(new FormattableMarkup('Error generating PDF: ' . Xss::filter($e->getMessage()), []), 'error'); + catch (PrintEngineException $e) { + drupal_set_message(new FormattableMarkup('Error generating Print: ' . Xss::filter($e->getMessage()), []), 'error'); return new RedirectResponse($entity->toUrl()->toString()); } } diff --git a/src/Entity/PdfEngine.php b/src/Entity/PdfEngine.php deleted file mode 100644 index 1e2db4e..0000000 --- a/src/Entity/PdfEngine.php +++ /dev/null @@ -1,95 +0,0 @@ -settings; - } - - /** - * {@inheritdoc} - */ - public function setSettings(array $settings) { - $this->settings = $settings; - return $this; - } - - /** - * {@inheritdoc} - */ - public function getPdfEnginePluginCollection() { - if (!$this->pdfEnginePluginCollection) { - $this->pdfEnginePluginCollection = new DefaultSingleLazyPluginCollection($this->getPdfEnginePluginManager(), $this->id, $this->settings); - } - return $this->pdfEnginePluginCollection; - } - - /** - * {@inheritdoc} - */ - public function getPluginCollections() { - return ['settings' => $this->getPdfEnginePluginCollection()]; - } - - /** - * Gets the plugin manager. - * - * @return \Drupal\entity_print\Plugin\EntityPrintPluginManager - * The plugin manager instance. - */ - protected function getPdfEnginePluginManager() { - return \Drupal::service('plugin.manager.entity_print.pdf_engine'); - } - -} diff --git a/src/Entity/PdfEngineInterface.php b/src/Entity/PdfEngineInterface.php deleted file mode 100644 index 17d3e06..0000000 --- a/src/Entity/PdfEngineInterface.php +++ /dev/null @@ -1,41 +0,0 @@ -settings; + } + + /** + * {@inheritdoc} + */ + public function setSettings(array $settings) { + $this->settings = $settings; + return $this; + } + + /** + * {@inheritdoc} + */ + public function getPrintEnginePluginCollection() { + if (!$this->printEnginePluginCollection) { + $this->printEnginePluginCollection = new DefaultSingleLazyPluginCollection($this->getPrintEnginePluginManager(), $this->id, $this->settings); + } + return $this->printEnginePluginCollection; + } + + /** + * {@inheritdoc} + */ + public function getPluginCollections() { + return ['settings' => $this->getPrintEnginePluginCollection()]; + } + + /** + * Gets the plugin manager. + * + * @return \Drupal\entity_print\Plugin\EntityPrintPluginManager + * The plugin manager instance. + */ + protected function getPrintEnginePluginManager() { + return \Drupal::service('plugin.manager.entity_print.print_engine'); + } + +} diff --git a/src/Entity/PrintEngineInterface.php b/src/Entity/PrintEngineInterface.php new file mode 100644 index 0000000..0d465df --- /dev/null +++ b/src/Entity/PrintEngineInterface.php @@ -0,0 +1,41 @@ +rendererFactory = $renderer_factory; - $this->dispatcher = $event_dispatcher; - } - - /** - * {@inheritdoc} - */ - public function getEntityRenderedAsPrint(EntityInterface $entity, PrintEngineInterface $print_engine, $force_download = FALSE, $use_default_css = TRUE) { - $print_engine->addPage($this->rendererFactory->create($entity)->getHtml($entity, $use_default_css, TRUE)); - - // Allow other modules to alter the generated Print object. - $this->dispatcher->dispatch(PdfEvents::PRE_SEND, new PreSendPdfEvent($print_engine, $entity)); - - // If we're forcing a download we need a filename otherwise it's just sent - // straight to the browser. - $filename = $force_download ? $this->generateFilename($entity) : NULL; - - return $print_engine->send($filename); - } - - /** - * {@inheritdoc} - */ - public function getMultipleEntitiesRenderedAsPrint(array $entities, PrintEngineInterface $print_engine, $force_download = FALSE, $use_default_css = TRUE) { - $print_engine->addPage($this->rendererFactory->create($entities)->getHtmlMultiple($entities, $use_default_css, TRUE)); - - // Allow other modules to alter the generated Print object. - $this->dispatcher->dispatch(PdfEvents::PRE_SEND_MULTIPLE, new PreSendPdfMultipleEvent($print_engine, $entities)); - - // If we're forcing a download we need a filename otherwise it's just sent - // straight to the browser. - $filename = $force_download ? $this->generateMultiFilename($entities) : NULL; - - return $print_engine->send($filename); - } - - /** - * {@inheritdoc} - */ - public function getEntityRenderedAsHtml(EntityInterface $entity, $use_default_css = TRUE, $optimize_css = TRUE) { - return $this->rendererFactory->create($entity)->getHtml($entity, $use_default_css, $optimize_css); - } - - /** - * Generate a filename from the entity. - * - * @param \Drupal\Core\Entity\EntityInterface $entity - * The content entity to generate the filename. - * @param bool $with_extension - * Allow us to exclude the Print file extension when generating the filename. - * - * @return string - * The cleaned filename from the entity label. - */ - protected function generateFilename(EntityInterface $entity, $with_extension = TRUE) { - $filename = preg_replace("/[^A-Za-z0-9 ]/", '', $entity->label()); - // If for some bizarre reason there isn't a valid character in the entity - // title or the entity doesn't provide a label then we use the entity type. - if (!$filename) { - $filename = $entity->getEntityTypeId(); - } - // @TODO abstract the .pdf extension. - return $with_extension ? $filename . '.pdf' : $filename; - } - - /** - * @param array $entities - * An array of entities to derive the filename for. - * - * @return string - * The filename to use. - */ - protected function generateMultiFilename(array $entities) { - $filename = ''; - foreach ($entities as $entity) { - $filename .= $this->generateFilename($entity, FALSE) . '-'; - } - return rtrim($filename, '-'); - } - -} diff --git a/src/Event/PdfCssAlterEvent.php b/src/Event/PdfCssAlterEvent.php deleted file mode 100644 index 8a5bf79..0000000 --- a/src/Event/PdfCssAlterEvent.php +++ /dev/null @@ -1,56 +0,0 @@ -build = &$build; - $this->entities = $entities; - } - - /** - * Gets the renderable array by reference if you want to change it. - * - * @return array - * The renderable array. - */ - public function &getBuild() { - return $this->build; - } - - /** - * Gets the entities being rendered. - * - * @return \Drupal\Core\Entity\EntityInterface[] - * An array of entities. - */ - public function getEntities() { - return $this->entities; - } - -} diff --git a/src/Event/PdfEventBase.php b/src/Event/PdfEventBase.php deleted file mode 100644 index b0589d1..0000000 --- a/src/Event/PdfEventBase.php +++ /dev/null @@ -1,35 +0,0 @@ -pdfEngine = $pdf_engine; - } - - /** - * Gets the PDF Engine plugin that will print the PDF. - * - * @return \Drupal\entity_print\Plugin\PdfEngineInterface - * The PDF Engine. - */ - public function getPdfEngine() { - return $this->pdfEngine; - } - -} diff --git a/src/Event/PdfEvents.php b/src/Event/PdfEvents.php deleted file mode 100644 index 80859ad..0000000 --- a/src/Event/PdfEvents.php +++ /dev/null @@ -1,78 +0,0 @@ -getBuild()['#attached']['library'][] = 'module/library'; - * @endcode - * - * @Event - * - * @see \Drupal\entity_print\Event\PdfCssAlterEvent - */ - const CSS_ALTER = 'entity_print.pdf.css_alter'; - - /** - * This event is fired right after the HTML has been generated. - * - * Any manipulations to the HTML string can happen here. You should normally - * avoid using this event and try and use the appropriate theme templates. We - * currently use this event to fix a core bug with absolute URLs. - * - * @Event - * - * @see \Drupal\entity_print\Event\PdfHtmlAlterEvent - */ - const POST_RENDER = 'entity_print.pdf.html_alter'; - -} diff --git a/src/Event/PdfHtmlAlterEvent.php b/src/Event/PdfHtmlAlterEvent.php deleted file mode 100644 index 72a262a..0000000 --- a/src/Event/PdfHtmlAlterEvent.php +++ /dev/null @@ -1,56 +0,0 @@ -html = &$html; - $this->entities = $entities; - } - - /** - * Gets the rendered HTML. - * - * @return string - * The HTML string. - */ - public function &getHtml() { - return $this->html; - } - - /** - * Gets the entities being rendered. - * - * @return \Drupal\Core\Entity\EntityInterface[] - * An array of entities. - */ - public function getEntities() { - return $this->entities; - } - -} diff --git a/src/Event/PreSendPdfEvent.php b/src/Event/PreSendPdfEvent.php deleted file mode 100644 index eefb72c..0000000 --- a/src/Event/PreSendPdfEvent.php +++ /dev/null @@ -1,41 +0,0 @@ -entity = $entity; - } - - /** - * Gets the entity that is being printed to PDF. - * - * @return \Drupal\Core\Entity\EntityInterface - * The content entity. - */ - public function getEntity() { - return $this->entity; - } - -} diff --git a/src/Event/PreSendPdfMultipleEvent.php b/src/Event/PreSendPdfMultipleEvent.php deleted file mode 100644 index 8d190db..0000000 --- a/src/Event/PreSendPdfMultipleEvent.php +++ /dev/null @@ -1,40 +0,0 @@ -entities = $entities; - } - - /** - * Gets the entities being printed to PDF. - * - * @return \Drupal\Core\Entity\EntityInterface[] - * The content entities. - */ - public function getEntities() { - return $this->entities; - } - -} diff --git a/src/Event/PreSendPrintEvent.php b/src/Event/PreSendPrintEvent.php new file mode 100644 index 0000000..bd54b38 --- /dev/null +++ b/src/Event/PreSendPrintEvent.php @@ -0,0 +1,41 @@ +entity = $entity; + } + + /** + * Gets the entity that is being printed to Print. + * + * @return \Drupal\Core\Entity\EntityInterface + * The content entity. + */ + public function getEntity() { + return $this->entity; + } + +} diff --git a/src/Event/PreSendPrintMultipleEvent.php b/src/Event/PreSendPrintMultipleEvent.php new file mode 100644 index 0000000..64228db --- /dev/null +++ b/src/Event/PreSendPrintMultipleEvent.php @@ -0,0 +1,40 @@ +entities = $entities; + } + + /** + * Gets the entities being printed to Print. + * + * @return \Drupal\Core\Entity\EntityInterface[] + * The content entities. + */ + public function getEntities() { + return $this->entities; + } + +} diff --git a/src/Event/PrintCssAlterEvent.php b/src/Event/PrintCssAlterEvent.php new file mode 100644 index 0000000..e1b3a9d --- /dev/null +++ b/src/Event/PrintCssAlterEvent.php @@ -0,0 +1,56 @@ +build = &$build; + $this->entities = $entities; + } + + /** + * Gets the renderable array by reference if you want to change it. + * + * @return array + * The renderable array. + */ + public function &getBuild() { + return $this->build; + } + + /** + * Gets the entities being rendered. + * + * @return \Drupal\Core\Entity\EntityInterface[] + * An array of entities. + */ + public function getEntities() { + return $this->entities; + } + +} diff --git a/src/Event/PrintEventBase.php b/src/Event/PrintEventBase.php new file mode 100644 index 0000000..e152e26 --- /dev/null +++ b/src/Event/PrintEventBase.php @@ -0,0 +1,35 @@ +printEngine = $print_engine; + } + + /** + * Gets the Print Engine plugin that will print the Print. + * + * @return \Drupal\entity_print\Plugin\PrintEngineInterface + * The Print Engine. + */ + public function getPrintEngine() { + return $this->printEngine; + } + +} diff --git a/src/Event/PrintEvents.php b/src/Event/PrintEvents.php new file mode 100644 index 0000000..494c88b --- /dev/null +++ b/src/Event/PrintEvents.php @@ -0,0 +1,78 @@ +getBuild()['#attached']['library'][] = 'module/library'; + * @endcode + * + * @Event + * + * @see \Drupal\entity_print\Event\PrintCssAlterEvent + */ + const CSS_ALTER = 'entity_print.print.css_alter'; + + /** + * This event is fired right after the HTML has been generated. + * + * Any manipulations to the HTML string can happen here. You should normally + * avoid using this event and try and use the appropriate theme templates. We + * currently use this event to fix a core bug with absolute URLs. + * + * @Event + * + * @see \Drupal\entity_print\Event\PrintHtmlAlterEvent + */ + const POST_RENDER = 'entity_print.print.html_alter'; + +} diff --git a/src/Event/PrintHtmlAlterEvent.php b/src/Event/PrintHtmlAlterEvent.php new file mode 100644 index 0000000..36d0a28 --- /dev/null +++ b/src/Event/PrintHtmlAlterEvent.php @@ -0,0 +1,56 @@ +html = &$html; + $this->entities = $entities; + } + + /** + * Gets the rendered HTML. + * + * @return string + * The HTML string. + */ + public function &getHtml() { + return $this->html; + } + + /** + * Gets the entities being rendered. + * + * @return \Drupal\Core\Entity\EntityInterface[] + * An array of entities. + */ + public function getEntities() { + return $this->entities; + } + +} diff --git a/src/EventSubscriber/PostRenderSubscriber.php b/src/EventSubscriber/PostRenderSubscriber.php index 7646c45..430a927 100644 --- a/src/EventSubscriber/PostRenderSubscriber.php +++ b/src/EventSubscriber/PostRenderSubscriber.php @@ -4,8 +4,8 @@ namespace Drupal\entity_print\EventSubscriber; use Drupal\Component\Utility\Unicode; use Drupal\Core\Config\ConfigFactoryInterface; -use Drupal\entity_print\Event\PdfEvents; -use Drupal\entity_print\Event\PdfHtmlAlterEvent; +use Drupal\entity_print\Event\PrintEvents; +use Drupal\entity_print\Event\PrintHtmlAlterEvent; use Masterminds\HTML5; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpFoundation\RequestStack; @@ -48,13 +48,13 @@ class PostRenderSubscriber implements EventSubscriberInterface { * This is a temporary workaround for a core issue. * @see https://drupal.org/node/1494670 * - * @param \Drupal\entity_print\Event\PdfHtmlAlterEvent $event + * @param \Drupal\entity_print\Event\PrintHtmlAlterEvent $event * The event object. */ - public function postRender(PdfHtmlAlterEvent $event) { + public function postRender(PrintHtmlAlterEvent $event) { // We only apply the fix to PHP Wkhtmltopdf because the other implementations // allow us to specify a base url. - if ($this->configFactory->get('pdf_engine') !== 'phpwkhtmltopdf') { + if ($this->configFactory->get('print_engine') !== 'phpwkhtmltopdf') { return; } @@ -91,7 +91,7 @@ class PostRenderSubscriber implements EventSubscriberInterface { * {@inheritdoc} */ public static function getSubscribedEvents() { - return [PdfEvents::POST_RENDER => 'postRender']; + return [PrintEvents::POST_RENDER => 'postRender']; } } diff --git a/src/Form/SettingsForm.php b/src/Form/SettingsForm.php index f4816e6..ed8489e 100644 --- a/src/Form/SettingsForm.php +++ b/src/Form/SettingsForm.php @@ -21,7 +21,7 @@ use Drupal\Core\Form\FormStateInterface; class SettingsForm extends ConfigFormBase { /** - * The Pdf engine plugin manager. + * The Print engine plugin manager. * * @var \Drupal\entity_print\Plugin\EntityPrintPluginManager */ @@ -56,8 +56,8 @@ class SettingsForm extends ConfigFormBase { public static function create(ContainerInterface $container) { return new static( $container->get('config.factory'), - $container->get('plugin.manager.entity_print.pdf_engine'), - $container->get('entity_type.manager')->getStorage('pdf_engine') + $container->get('plugin.manager.entity_print.print_engine'), + $container->get('entity_type.manager')->getStorage('print_engine') ); } @@ -82,17 +82,17 @@ class SettingsForm extends ConfigFormBase { */ public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL) { $disabled_engines = []; - $pdf_engines = []; + $print_engines = []; foreach ($this->pluginManager->getDefinitions() as $plugin_id => $definition) { - /** @var \Drupal\entity_print\Plugin\PdfEngineInterface $class */ + /** @var \Drupal\entity_print\Plugin\PrintEngineInterface $class */ $class = $definition['class']; if ($class::dependenciesAvailable()) { - $pdf_engines[$plugin_id] = $definition['label']; + $print_engines[$plugin_id] = $definition['label']; } else { $disabled_engines[$plugin_id] = $definition['label']; - // Show the user which PDF engines are disabled, but only for the page load + // Show the user which Print engines are disabled, but only for the page load // not on AJAX requests. if (!$request->isXmlHttpRequest()) { drupal_set_message($this->t('@name is not available because it is not configured. @installation.', [ @@ -117,37 +117,37 @@ class SettingsForm extends ConfigFormBase { $form['entity_print']['force_download'] = [ '#type' => 'checkbox', '#title' => $this->t('Force Download'), - '#description' => $this->t('This option will attempt to force the browser to download the PDF with a filename from the node title.'), + '#description' => $this->t('This option will attempt to force the browser to download the Print with a filename from the node title.'), '#default_value' => $config->get('force_download'), ]; - $form['entity_print']['pdf_engine'] = [ + $form['entity_print']['print_engine'] = [ '#type' => 'select', - '#title' => $this->t('Pdf Engine'), - '#description' => $this->t('Select the PDF engine to render the PDF'), - '#options' => $pdf_engines, - '#default_value' => $config->get('pdf_engine'), + '#title' => $this->t('Print Engine'), + '#description' => $this->t('Select the Print engine to render the Print'), + '#options' => $print_engines, + '#default_value' => $config->get('print_engine'), '#empty_option' => $this->t('- None -'), '#ajax' => [ 'callback' => '::ajaxPluginFormCallback', - 'wrapper' => 'pdf-engine-config', + 'wrapper' => 'print-engine-config', 'effect' => 'fade', ], ]; - $form['entity_print']['pdf_engine_config'] = [ + $form['entity_print']['print_engine_config'] = [ '#type' => 'container', - '#id' => 'pdf-engine-config', + '#id' => 'print-engine-config', ]; - // If we have a pdf_engine in the form_state then use that otherwise, fall + // If we have a print_engine in the form_state then use that otherwise, fall // back to what was saved as this is a fresh form. Check explicitly for NULL // in case they selected the None option which is false'y. - $plugin_id = !is_null($form_state->getValue('pdf_engine')) ? $form_state->getValue('pdf_engine') : $config->get('pdf_engine'); + $plugin_id = !is_null($form_state->getValue('print_engine')) ? $form_state->getValue('print_engine') : $config->get('print_engine'); // If we have a plugin id and the plugin hasn't since been disabled then we // load the config for the plugin. if ($plugin_id && !in_array($plugin_id, array_keys($disabled_engines), TRUE)) { - $form['entity_print']['pdf_engine_config'][$plugin_id] = $this->getPluginForm($plugin_id, $form_state); + $form['entity_print']['print_engine_config'][$plugin_id] = $this->getPluginForm($plugin_id, $form_state); } return parent::buildForm($form, $form_state); @@ -157,7 +157,7 @@ class SettingsForm extends ConfigFormBase { * Ajax form callback. */ public function ajaxPluginFormCallback(&$form, FormStateInterface $form_state) { - return $form['entity_print']['pdf_engine_config']; + return $form['entity_print']['print_engine_config']; } /** @@ -186,12 +186,12 @@ class SettingsForm extends ConfigFormBase { public function validateForm(array &$form, FormStateInterface $form_state) { parent::validateForm($form, $form_state); - if ($plugin_id = $form_state->getValue('pdf_engine')) { + if ($plugin_id = $form_state->getValue('print_engine')) { // Load the config entity, submit the relevant plugin form and then save // it. $entity = $this->loadConfigEntity($plugin_id); - /** @var \Drupal\entity_print\Plugin\PdfEngineInterface $plugin */ - $plugin = $entity->getPdfEnginePluginCollection()->get($entity->id()); + /** @var \Drupal\entity_print\Plugin\PrintEngineInterface $plugin */ + $plugin = $entity->getPrintEnginePluginCollection()->get($entity->id()); $plugin->validateConfigurationForm($form, $form_state); } } @@ -200,12 +200,12 @@ class SettingsForm extends ConfigFormBase { * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { - if ($plugin_id = $form_state->getValue('pdf_engine')) { + if ($plugin_id = $form_state->getValue('print_engine')) { // Load the config entity, submit the relevant plugin form and then save // it. $entity = $this->loadConfigEntity($plugin_id); - /** @var \Drupal\entity_print\Plugin\PdfEngineInterface $plugin */ - $plugin = $entity->getPdfEnginePluginCollection()->get($entity->id()); + /** @var \Drupal\entity_print\Plugin\PrintEngineInterface $plugin */ + $plugin = $entity->getPrintEnginePluginCollection()->get($entity->id()); $plugin->submitConfigurationForm($form, $form_state); $entity->save(); } @@ -215,7 +215,7 @@ class SettingsForm extends ConfigFormBase { $this->config('entity_print.settings') ->set('default_css', $values['default_css']) ->set('force_download', $values['force_download']) - ->set('pdf_engine', $values['pdf_engine']) + ->set('print_engine', $values['print_engine']) ->save(); } @@ -223,9 +223,9 @@ class SettingsForm extends ConfigFormBase { * Gets the config entity backing the specified plugin. * * @param string $plugin_id - * The PDF engine plugin id. + * The Print engine plugin id. * - * @return \Drupal\entity_print\Entity\PdfEngine + * @return \Drupal\entity_print\Entity\PrintEngine * The loaded config object backing the plugin. */ protected function loadConfigEntity($plugin_id) { diff --git a/src/PdfEngineException.php b/src/PdfEngineException.php deleted file mode 100644 index 865ad67..0000000 --- a/src/PdfEngineException.php +++ /dev/null @@ -1,15 +0,0 @@ -accessManager = $access_manager; - $this->pdfBuilder = $pdf_builder; + $this->printBuilder = $print_builder; $this->pluginManager = $plugin_manager; $this->entityPrintConfig = $entity_print_config; } @@ -90,7 +90,7 @@ class PdfDownload extends ActionBase implements ContainerFactoryPluginInterface $plugin_definition, $container->get('access_manager'), $container->get('entity_print.print_manager'), - $container->get('plugin.manager.entity_print.pdf_engine'), + $container->get('plugin.manager.entity_print.print_engine'), $container->get('config.factory')->get('entity_print.settings') ); } @@ -112,7 +112,7 @@ class PdfDownload extends ActionBase implements ContainerFactoryPluginInterface */ public function execute($entity = NULL) { $this->sendResponse((function() use ($entity) { - $this->pdfBuilder->getEntityRenderedAsPdf($entity, $this->getPdfEngine(), TRUE); + $this->printBuilder->printSingle($entity, $this->getPrintEngine(), TRUE); })); } @@ -121,7 +121,7 @@ class PdfDownload extends ActionBase implements ContainerFactoryPluginInterface */ public function executeMultiple(array $entities) { $this->sendResponse((function() use ($entities) { - $this->pdfBuilder->getMultipleEntitiesRenderedAsPdf($entities, $this->getPdfEngine(), TRUE); + $this->printBuilder->printMultiple($entities, $this->getPrintEngine(), TRUE); })); } @@ -135,22 +135,22 @@ class PdfDownload extends ActionBase implements ContainerFactoryPluginInterface try { (new StreamedResponse($callback))->send(); } - catch (PdfEngineException $e) { + catch (PrintEngineException $e) { drupal_set_message(new FormattableMarkup(Xss::filter($e->getMessage()), []), 'error'); } } /** - * Gets the PDF engine implementation. + * Gets the Print engine implementation. * - * @return \Drupal\entity_print\Plugin\PdfEngineInterface - * The PDF Engine implementation. + * @return \Drupal\entity_print\Plugin\PrintEngineInterface + * The Print Engine implementation. */ - protected function getPdfEngine() { - if (!isset($this->pdfEngine)) { - $this->pdfEngine = $this->pluginManager->createInstance($this->entityPrintConfig->get('pdf_engine')); + protected function getPrintEngine() { + if (!isset($this->printEngine)) { + $this->printEngine = $this->pluginManager->createInstance($this->entityPrintConfig->get('print_engine')); } - return $this->pdfEngine; + return $this->printEngine; } } diff --git a/src/Plugin/EntityPrint/PdfEngine/DomPdf.php b/src/Plugin/EntityPrint/PdfEngine/DomPdf.php deleted file mode 100644 index 171f5d9..0000000 --- a/src/Plugin/EntityPrint/PdfEngine/DomPdf.php +++ /dev/null @@ -1,163 +0,0 @@ -pdf = new DompdfLib($this->configuration); - $this->pdf - ->setBaseHost($request->getHttpHost()) - ->setProtocol($request->getScheme() . '://'); - } - - /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { - return new static( - $configuration, - $plugin_id, - $plugin_definition, - $container->get('request_stack')->getCurrentRequest() - ); - } - - /** - * {@inheritdoc} - */ - public static function getInstallationInstructions() { - return t('Please install with: @command', ['@command' => 'composer require "dompdf/dompdf 0.7.0-beta3"']); - } - - /** - * {@inheritdoc} - */ - public function defaultConfiguration() { - return [ - 'enable_html5_parser' => TRUE, - 'enable_remote' => TRUE, - 'default_paper_size' => 'letter', - ]; - } - - /** - * {@inheritdoc} - */ - public function buildConfigurationForm(array $form, FormStateInterface $form_state) { - $paper_sizes = array_combine(array_keys(CPDF::$PAPER_SIZES), array_map(function($value) { - return ucfirst($value); - }, array_keys(CPDF::$PAPER_SIZES))); - $form['default_paper_size'] = [ - '#title' => $this->t('Paper Size'), - '#type' => 'select', - '#options' => $paper_sizes, - '#default_value' => $this->configuration['default_paper_size'], - '#description' => $this->t('The page size to print the PDF to.'), - ]; - $form['enable_html5_parser'] = [ - '#title' => $this->t('Enable HTML5 Parser'), - '#type' => 'checkbox', - '#default_value' => $this->configuration['enable_html5_parser'], - '#description' => $this->t('Note, this library doesn\'t work without this option enabled.'), - ]; - $form['enable_remote'] = [ - '#title' => $this->t('Enable Remote URLs'), - '#type' => 'checkbox', - '#default_value' => $this->configuration['enable_remote'], - '#description' => $this->t('This settings must be enabled for CSS and Images to work unless you manipulate the source manually.'), - ]; - - return $form; - } - - /** - * {@inheritdoc} - */ - public function addPage($content) { - // We must keep adding to previously added HTML as loadHtml() replaces the - // entire document. - $this->html .= (string) $content; - $this->pdf->loadHtml($this->html); - } - - /** - * {@inheritdoc} - */ - public function send($filename = NULL) { - $this->pdf->render(); - - // Dompdf doesn't have a return value for send so just check the error - // global it provides. - if ($errors = $this->getError()) { - throw new PdfEngineException(sprintf('Failed to generate PDF: %s', $errors)); - } - - // The Dompdf library internally adds the .pdf extension so we remove it - // from our filename here. - $filename = preg_replace('/\.pdf$/i', '', $filename); - - $this->pdf->stream($filename); - } - - /** - * {@inheritdoc} - */ - protected function getError() { - global $_dompdf_warnings; - if (is_array($_dompdf_warnings)) { - return implode(', ', $_dompdf_warnings); - } - return FALSE; - } - - /** - * {@inheritdoc} - */ - public static function dependenciesAvailable() { - return class_exists('Dompdf\Dompdf'); - } - -} diff --git a/src/Plugin/EntityPrint/PdfEngine/PhpWkhtmlToPdf.php b/src/Plugin/EntityPrint/PdfEngine/PhpWkhtmlToPdf.php deleted file mode 100644 index f9c068f..0000000 --- a/src/Plugin/EntityPrint/PdfEngine/PhpWkhtmlToPdf.php +++ /dev/null @@ -1,106 +0,0 @@ -pdf = new Pdf(['binary' => $this->configuration['binary_location']]); - } - - /** - * {@inheritdoc} - */ - public static function getInstallationInstructions() { - return t('Please install with: @command', ['@command' => 'composer require "mikehaertl/phpwkhtmltopdf ~2.1"']); - } - - /** - * {@inheritdoc} - */ - public function defaultConfiguration() { - return [ - 'binary_location' => '/usr/local/bin/wkhtmltopdf', - ]; - } - - /** - * {@inheritdoc} - */ - public function buildConfigurationForm(array $form, FormStateInterface $form_state) { - $form = parent::buildConfigurationForm($form, $form_state); - $form['binary_location'] = [ - '#type' => 'textfield', - '#title' => $this->t('Binary Location'), - '#description' => $this->t('Set this to the system path where the PDF engine binary is located.'), - '#default_value' => $this->configuration['binary_location'], - ]; - return $form; - } - - /** - * {@inheritdoc} - */ - public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { - parent::validateConfigurationForm($form, $form_state); - $binary_location = $form_state->getValue('binary_location'); - if (!file_exists($binary_location)) { - $form_state->setErrorByName('binary_location', sprintf('The wkhtmltopdf binary does not exist at %s', $binary_location)); - } - } - - /** - * {@inheritdoc} - */ - public function send($filename = NULL) { - if (!$this->pdf->send($filename)) { - throw new PdfEngineException(sprintf('Failed to generate PDF: %s', $this->pdf->getError())); - } - } - - /** - * {@inheritdoc} - */ - public function addPage($content) { - $this->pdf->addPage($content); - } - - /** - * {@inheritdoc} - */ - public static function dependenciesAvailable() { - return class_exists('mikehaertl\wkhtmlto\Pdf'); - } - -} diff --git a/src/Plugin/EntityPrint/PrintEngine/DomPdf.php b/src/Plugin/EntityPrint/PrintEngine/DomPdf.php new file mode 100644 index 0000000..4427e17 --- /dev/null +++ b/src/Plugin/EntityPrint/PrintEngine/DomPdf.php @@ -0,0 +1,164 @@ +pdf = new DompdfLib($this->configuration); + $this->pdf + ->setBaseHost($request->getHttpHost()) + ->setProtocol($request->getScheme() . '://'); + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('request_stack')->getCurrentRequest() + ); + } + + /** + * {@inheritdoc} + */ + public static function getInstallationInstructions() { + return t('Please install with: @command', ['@command' => 'composer require "dompdf/dompdf 0.7.0-beta3"']); + } + + /** + * {@inheritdoc} + */ + public function defaultConfiguration() { + return [ + 'enable_html5_parser' => TRUE, + 'enable_remote' => TRUE, + 'default_paper_size' => 'letter', + ]; + } + + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(array $form, FormStateInterface $form_state) { + $paper_sizes = array_combine(array_keys(CPDF::$PAPER_SIZES), array_map(function($value) { + return ucfirst($value); + }, array_keys(CPDF::$PAPER_SIZES))); + $form['default_paper_size'] = [ + '#title' => $this->t('Paper Size'), + '#type' => 'select', + '#options' => $paper_sizes, + '#default_value' => $this->configuration['default_paper_size'], + '#description' => $this->t('The page size to print the PDF to.'), + ]; + $form['enable_html5_parser'] = [ + '#title' => $this->t('Enable HTML5 Parser'), + '#type' => 'checkbox', + '#default_value' => $this->configuration['enable_html5_parser'], + '#description' => $this->t('Note, this library doesn\'t work without this option enabled.'), + ]; + $form['enable_remote'] = [ + '#title' => $this->t('Enable Remote URLs'), + '#type' => 'checkbox', + '#default_value' => $this->configuration['enable_remote'], + '#description' => $this->t('This settings must be enabled for CSS and Images to work unless you manipulate the source manually.'), + ]; + + return $form; + } + + /** + * {@inheritdoc} + */ + public function addPage($content) { + // We must keep adding to previously added HTML as loadHtml() replaces the + // entire document. + $this->html .= (string) $content; + $this->pdf->loadHtml($this->html); + } + + /** + * {@inheritdoc} + */ + public function send($filename = NULL) { + $this->pdf->render(); + + // Dompdf doesn't have a return value for send so just check the error + // global it provides. + if ($errors = $this->getError()) { + throw new PrintEngineException(sprintf('Failed to generate PDF: %s', $errors)); + } + + // The Dompdf library internally adds the .pdf extension so we remove it + // from our filename here. + $filename = preg_replace('/\.pdf$/i', '', $filename); + + $this->pdf->stream($filename); + } + + /** + * {@inheritdoc} + */ + protected function getError() { + global $_dompdf_warnings; + if (is_array($_dompdf_warnings)) { + return implode(', ', $_dompdf_warnings); + } + return FALSE; + } + + /** + * {@inheritdoc} + */ + public static function dependenciesAvailable() { + return class_exists('Dompdf\Dompdf'); + } + +} diff --git a/src/Plugin/EntityPrint/PrintEngine/PhpWkhtmlToPdf.php b/src/Plugin/EntityPrint/PrintEngine/PhpWkhtmlToPdf.php new file mode 100644 index 0000000..f96f6a7 --- /dev/null +++ b/src/Plugin/EntityPrint/PrintEngine/PhpWkhtmlToPdf.php @@ -0,0 +1,107 @@ +pdf = new Pdf(['binary' => $this->configuration['binary_location']]); + } + + /** + * {@inheritdoc} + */ + public static function getInstallationInstructions() { + return t('Please install with: @command', ['@command' => 'composer require "mikehaertl/phpwkhtmltopdf ~2.1"']); + } + + /** + * {@inheritdoc} + */ + public function defaultConfiguration() { + return [ + 'binary_location' => '/usr/local/bin/wkhtmltopdf', + ]; + } + + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(array $form, FormStateInterface $form_state) { + $form = parent::buildConfigurationForm($form, $form_state); + $form['binary_location'] = [ + '#type' => 'textfield', + '#title' => $this->t('Binary Location'), + '#description' => $this->t('Set this to the system path where the PDF engine binary is located.'), + '#default_value' => $this->configuration['binary_location'], + ]; + return $form; + } + + /** + * {@inheritdoc} + */ + public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { + parent::validateConfigurationForm($form, $form_state); + $binary_location = $form_state->getValue('binary_location'); + if (!file_exists($binary_location)) { + $form_state->setErrorByName('binary_location', sprintf('The wkhtmltopdf binary does not exist at %s', $binary_location)); + } + } + + /** + * {@inheritdoc} + */ + public function send($filename = NULL) { + if (!$this->pdf->send($filename)) { + throw new PrintEngineException(sprintf('Failed to generate PDF: %s', $this->pdf->getError())); + } + } + + /** + * {@inheritdoc} + */ + public function addPage($content) { + $this->pdf->addPage($content); + } + + /** + * {@inheritdoc} + */ + public static function dependenciesAvailable() { + return class_exists('mikehaertl\wkhtmlto\Pdf'); + } + +} diff --git a/src/Plugin/EntityPrintPluginManager.php b/src/Plugin/EntityPrintPluginManager.php index c4d40fa..b6555b1 100644 --- a/src/Plugin/EntityPrintPluginManager.php +++ b/src/Plugin/EntityPrintPluginManager.php @@ -5,8 +5,8 @@ namespace Drupal\entity_print\Plugin; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Plugin\DefaultPluginManager; -use Drupal\entity_print\Event\PdfEvents; -use Drupal\entity_print\PdfEngineException; +use Drupal\entity_print\Event\PrintEvents; +use Drupal\entity_print\PrintEngineException; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\GenericEvent; @@ -31,9 +31,9 @@ class EntityPrintPluginManager extends DefaultPluginManager { * The module handler to invoke the alter hook with. */ public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, EventDispatcherInterface $dispatcher) { - parent::__construct('Plugin/EntityPrint/PdfEngine', $namespaces, $module_handler, 'Drupal\entity_print\Plugin\PdfEngineInterface', 'Drupal\entity_print\Annotation\PdfEngine'); - $this->alterInfo('entity_print_pdf_engine'); - $this->setCacheBackend($cache_backend, 'entity_print_pdf_engines'); + parent::__construct('Plugin/EntityPrint/PrintEngine', $namespaces, $module_handler, 'Drupal\entity_print\Plugin\PrintEngineInterface', 'Drupal\entity_print\Annotation\PrintEngine'); + $this->alterInfo('entity_print_print_engine'); + $this->setCacheBackend($cache_backend, 'entity_print_print_engines'); $this->dispatcher = $dispatcher; } @@ -41,16 +41,16 @@ class EntityPrintPluginManager extends DefaultPluginManager { * {@inheritdoc} */ public function createInstance($plugin_id, array $configuration = []) { - $configuration = array_merge($this->getPdfEngineSettings($plugin_id), $configuration); + $configuration = array_merge($this->getPrintEngineSettings($plugin_id), $configuration); - /** @var \Drupal\entity_print\Plugin\PdfEngineInterface $class */ + /** @var \Drupal\entity_print\Plugin\PrintEngineInterface $class */ $definition = $this->getDefinition($plugin_id); $class = $definition['class']; // Throw an exception if someone tries to use a plugin that doesn't have all // of its dependencies met. if (!$class::dependenciesAvailable()) { - throw new PdfEngineException(sprintf('Missing dependencies. %s', $class::getInstallationInstructions())); + throw new PrintEngineException(sprintf('Missing dependencies. %s', $class::getInstallationInstructions())); } return parent::createInstance($plugin_id, $configuration); @@ -63,17 +63,17 @@ class EntityPrintPluginManager extends DefaultPluginManager { * The plugin id. * * @return array - * An array of PDF engine settings for this plugin. + * An array of Print engine settings for this plugin. */ - protected function getPdfEngineSettings($plugin_id) { - /** @var \Drupal\entity_print\Entity\PdfEngineInterface $storage */ - $storage = \Drupal::entityTypeManager()->getStorage('pdf_engine'); + protected function getPrintEngineSettings($plugin_id) { + /** @var \Drupal\entity_print\Entity\PrintEngineInterface $storage */ + $storage = \Drupal::entityTypeManager()->getStorage('print_engine'); if (!$entity = $storage->load($plugin_id)) { $entity = $storage->create(['id' => $plugin_id]); } $configuration = $entity->getSettings(); - $event = new GenericEvent(PdfEvents::CONFIGURATION_ALTER, ['configuration' => $configuration, 'config' => $entity]); - $this->dispatcher->dispatch(PdfEvents::CONFIGURATION_ALTER, $event); + $event = new GenericEvent(PrintEvents::CONFIGURATION_ALTER, ['configuration' => $configuration, 'config' => $entity]); + $this->dispatcher->dispatch(PrintEvents::CONFIGURATION_ALTER, $event); $configuration = $event->getArgument('configuration'); return $configuration; diff --git a/src/Plugin/PdfEngineBase.php b/src/Plugin/PdfEngineBase.php deleted file mode 100644 index b14aa41..0000000 --- a/src/Plugin/PdfEngineBase.php +++ /dev/null @@ -1,92 +0,0 @@ -setConfiguration($configuration); - } - - /** - * {@inheritdoc} - */ - public static function getInstallationInstructions() { - return 'Please install using composer'; - } - - /** - * {@inheritdoc} - */ - public function buildConfigurationForm(array $form, FormStateInterface $form_state) { - - return $form; - } - - /** - * {@inheritdoc} - */ - public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { - // No validation required by default. - } - - /** - * {@inheritdoc} - */ - public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { - foreach ($this->defaultConfiguration() as $key => $value) { - $this->configuration[$key] = $form_state->getValue($key); - } - } - - /** - * {@inheritdoc} - */ - public function getConfiguration() { - return $this->configuration; - } - - /** - * {@inheritdoc} - */ - public function setConfiguration(array $configuration) { - $this->configuration = $configuration + $this->defaultConfiguration(); - } - - /** - * {@inheritdoc} - */ - public function defaultConfiguration() { - return []; - } - - /** - * {@inheritdoc} - */ - public function calculateDependencies() { - return []; - } - -} diff --git a/src/Plugin/PdfEngineInterface.php b/src/Plugin/PdfEngineInterface.php deleted file mode 100644 index 9962560..0000000 --- a/src/Plugin/PdfEngineInterface.php +++ /dev/null @@ -1,53 +0,0 @@ -setConfiguration($configuration); + } + + /** + * {@inheritdoc} + */ + public static function getInstallationInstructions() { + return 'Please install using composer'; + } + + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(array $form, FormStateInterface $form_state) { + + return $form; + } + + /** + * {@inheritdoc} + */ + public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { + // No validation required by default. + } + + /** + * {@inheritdoc} + */ + public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { + foreach ($this->defaultConfiguration() as $key => $value) { + $this->configuration[$key] = $form_state->getValue($key); + } + } + + /** + * {@inheritdoc} + */ + public function getConfiguration() { + return $this->configuration; + } + + /** + * {@inheritdoc} + */ + public function setConfiguration(array $configuration) { + $this->configuration = $configuration + $this->defaultConfiguration(); + } + + /** + * {@inheritdoc} + */ + public function defaultConfiguration() { + return []; + } + + /** + * {@inheritdoc} + */ + public function calculateDependencies() { + return []; + } + +} diff --git a/src/Plugin/PrintEngineInterface.php b/src/Plugin/PrintEngineInterface.php new file mode 100644 index 0000000..1740477 --- /dev/null +++ b/src/Plugin/PrintEngineInterface.php @@ -0,0 +1,53 @@ +rendererFactory = $renderer_factory; + $this->dispatcher = $event_dispatcher; + } + + /** + * {@inheritdoc} + */ + public function printSingle(EntityInterface $entity, PrintEngineInterface $print_engine, $force_download = FALSE, $use_default_css = TRUE) { + $print_engine->addPage($this->rendererFactory->create($entity)->getHtml($entity, $use_default_css, TRUE)); + + // Allow other modules to alter the generated Print object. + $this->dispatcher->dispatch(PrintEvents::PRE_SEND, new PreSendPrintEvent($print_engine, $entity)); + + // If we're forcing a download we need a filename otherwise it's just sent + // straight to the browser. + $filename = $force_download ? $this->generateFilename($entity) : NULL; + + return $print_engine->send($filename); + } + + /** + * {@inheritdoc} + */ + public function printMultiple(array $entities, PrintEngineInterface $print_engine, $force_download = FALSE, $use_default_css = TRUE) { + $print_engine->addPage($this->rendererFactory->create($entities)->getHtmlMultiple($entities, $use_default_css, TRUE)); + + // Allow other modules to alter the generated Print object. + $this->dispatcher->dispatch(PrintEvents::PRE_SEND_MULTIPLE, new PreSendPrintMultipleEvent($print_engine, $entities)); + + // If we're forcing a download we need a filename otherwise it's just sent + // straight to the browser. + $filename = $force_download ? $this->generateMultiFilename($entities) : NULL; + + return $print_engine->send($filename); + } + + /** + * {@inheritdoc} + */ + public function printHtml(EntityInterface $entity, $use_default_css = TRUE, $optimize_css = TRUE) { + return $this->rendererFactory->create($entity)->getHtml($entity, $use_default_css, $optimize_css); + } + + /** + * Generate a filename from the entity. + * + * @param \Drupal\Core\Entity\EntityInterface $entity + * The content entity to generate the filename. + * @param bool $with_extension + * Allow us to exclude the Print file extension when generating the filename. + * + * @return string + * The cleaned filename from the entity label. + */ + protected function generateFilename(EntityInterface $entity, $with_extension = TRUE) { + $filename = preg_replace("/[^A-Za-z0-9 ]/", '', $entity->label()); + // If for some bizarre reason there isn't a valid character in the entity + // title or the entity doesn't provide a label then we use the entity type. + if (!$filename) { + $filename = $entity->getEntityTypeId(); + } + // @TODO abstract the .pdf extension. + return $with_extension ? $filename . '.pdf' : $filename; + } + + /** + * @param array $entities + * An array of entities to derive the filename for. + * + * @return string + * The filename to use. + */ + protected function generateMultiFilename(array $entities) { + $filename = ''; + foreach ($entities as $entity) { + $filename .= $this->generateFilename($entity, FALSE) . '-'; + } + return rtrim($filename, '-'); + } + +} diff --git a/src/PrintBuilderInterface.php b/src/PrintBuilderInterface.php index 63eeeef..93b8a07 100644 --- a/src/PrintBuilderInterface.php +++ b/src/PrintBuilderInterface.php @@ -25,7 +25,7 @@ interface PrintBuilderInterface { * @return string * FALSE or the Print content will be sent to the browser. */ - public function getEntityRenderedAsPrint(EntityInterface $entity, PrintEngineInterface $print_engine, $force_download = FALSE, $use_default_css = TRUE); + public function printSingle(EntityInterface $entity, PrintEngineInterface $print_engine, $force_download = FALSE, $use_default_css = TRUE); /** * Render any content entity as a Print. @@ -42,7 +42,7 @@ interface PrintBuilderInterface { * @return string * FALSE or the Print content will be sent to the browser. */ - public function getMultipleEntitiesRenderedAsPrint(array $entities, PrintEngineInterface $print_engine, $force_download = FALSE, $use_default_css = TRUE); + public function printMultiple(array $entities, PrintEngineInterface $print_engine, $force_download = FALSE, $use_default_css = TRUE); /** * Get a HTML version of the entity as used for the Print rendering. @@ -57,5 +57,5 @@ interface PrintBuilderInterface { * @return string * The rendered HTML for this entity, the same as what is used for the Print. */ - public function getEntityRenderedAsHtml(EntityInterface $entity, $use_default_css = TRUE, $optimize_css = TRUE); + public function printHtml(EntityInterface $entity, $use_default_css = TRUE, $optimize_css = TRUE); } diff --git a/src/PrintEngineException.php b/src/PrintEngineException.php new file mode 100644 index 0000000..6bdff35 --- /dev/null +++ b/src/PrintEngineException.php @@ -0,0 +1,15 @@ +addCss($render, $entity); } - $this->dispatcher->dispatch(PdfEvents::CSS_ALTER, new PdfCssAlterEvent($render, $entities)); + $this->dispatcher->dispatch(PrintEvents::CSS_ALTER, new PrintCssAlterEvent($render, $entities)); $css_assets = $this->assetResolver->getCssAssets(AttachedAssets::createFromRenderArray($render), $optimize_css); $rendered_css = $this->cssRenderer->render($css_assets); $render['#entity_print_css'] = $this->renderer->render($rendered_css); @@ -96,7 +96,7 @@ class ContentEntityRenderer extends RendererBase { $html = (string) $this->renderer->render($render); // Allow other modules to alter the generated HTML. - $this->dispatcher->dispatch(PdfEvents::POST_RENDER, new PdfHtmlAlterEvent($html, $entities)); + $this->dispatcher->dispatch(PrintEvents::POST_RENDER, new PrintHtmlAlterEvent($html, $entities)); return $html; } diff --git a/src/Renderer/RendererFactory.php b/src/Renderer/RendererFactory.php index 04a11c8..5e28d4b 100644 --- a/src/Renderer/RendererFactory.php +++ b/src/Renderer/RendererFactory.php @@ -3,7 +3,7 @@ namespace Drupal\entity_print\Renderer; use Drupal\Core\Entity\EntityInterface; -use Drupal\entity_print\PdfEngineException; +use Drupal\entity_print\PrintEngineException; use Symfony\Component\DependencyInjection\ContainerAwareTrait; /** @@ -36,7 +36,7 @@ class RendererFactory implements RendererFactoryInterface { } } - throw new PdfEngineException(sprintf('Rendering not yet supported for "%s". Entity Print context "%s"', is_object($item) ? get_class($item) : $item, $context)); + throw new PrintEngineException(sprintf('Rendering not yet supported for "%s". Entity Print context "%s"', is_object($item) ? get_class($item) : $item, $context)); } } diff --git a/tests/modules/entity_print_test/src/EventSubscriber/TestEngineConfigurationAlter.php b/tests/modules/entity_print_test/src/EventSubscriber/TestEngineConfigurationAlter.php index 43ba007..e49db13 100644 --- a/tests/modules/entity_print_test/src/EventSubscriber/TestEngineConfigurationAlter.php +++ b/tests/modules/entity_print_test/src/EventSubscriber/TestEngineConfigurationAlter.php @@ -2,9 +2,9 @@ namespace Drupal\entity_print_test\EventSubscriber; -use Drupal\entity_print\Event\PdfCssAlterEvent; -use Drupal\entity_print\Event\PdfEvents; -use Drupal\entity_print\Event\PreSendPdfEvent; +use Drupal\entity_print\Event\PrintCssAlterEvent; +use Drupal\entity_print\Event\PrintEvents; +use Drupal\entity_print\Event\PreSendPrintEvent; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\GenericEvent; @@ -27,10 +27,10 @@ class TestEngineConfigurationAlter implements EventSubscriberInterface { /** * Alter the CSS renderable array and add our CSS. - * @param \Drupal\entity_print\Event\PdfCssAlterEvent $event + * @param \Drupal\entity_print\Event\PrintCssAlterEvent $event * The event object. */ - public function alterCss(PdfCssAlterEvent $event) { + public function alterCss(PrintCssAlterEvent $event) { $event->getBuild()['#attached']['library'][] = 'entity_print_test_theme/module'; } @@ -39,8 +39,8 @@ class TestEngineConfigurationAlter implements EventSubscriberInterface { */ public static function getSubscribedEvents() { return [ - PdfEvents::CONFIGURATION_ALTER => 'alterConfiguration', - PdfEvents::CSS_ALTER => 'alterCss', + PrintEvents::CONFIGURATION_ALTER => 'alterConfiguration', + PrintEvents::CSS_ALTER => 'alterCss', ]; } diff --git a/tests/modules/entity_print_test/src/Plugin/EntityPrint/PdfEngine/TestPdfEngine.php b/tests/modules/entity_print_test/src/Plugin/EntityPrint/PdfEngine/TestPdfEngine.php index cd9c1c5..6e36002 100644 --- a/tests/modules/entity_print_test/src/Plugin/EntityPrint/PdfEngine/TestPdfEngine.php +++ b/tests/modules/entity_print_test/src/Plugin/EntityPrint/PdfEngine/TestPdfEngine.php @@ -8,7 +8,7 @@ namespace Drupal\entity_print_test\Plugin\EntityPrint\PdfEngine; use Drupal\Core\Form\FormStateInterface; -use Drupal\entity_print\PdfEngineException; +use Drupal\entity_print\PrintEngineException; use Drupal\entity_print\Plugin\PdfEngineBase; /**