diff --git a/src/PrintBuilder.php b/src/PrintBuilder.php index 6026405..ba5caac 100644 --- a/src/PrintBuilder.php +++ b/src/PrintBuilder.php @@ -77,17 +77,19 @@ class PrintBuilder implements PrintBuilderInterface { /** * {@inheritdoc} */ - public function savePrintable(array $entities, PrintEngineInterface $print_engine, $uri, $use_default_css = TRUE) { + 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); // Allow other modules to alter the generated Print object. $this->dispatcher->dispatch(PrintEvents::PRE_SEND, new PreSendPrintEvent($print_engine, $entities)); // If we didn't have a URI passed in the generate one. - if (!$uri) { - $uri = file_default_scheme() . '://' . $renderer->getFilename($entities) . '.' . $print_engine->getExportType()->getFileExtension(); + if (!$filename) { + $filename = $renderer->getFilename($entities) . '.' . $print_engine->getExportType()->getFileExtension(); } + $uri = "$scheme://$filename"; + // Save the file. return file_unmanaged_save_data($print_engine->getBlob(), $uri, FILE_EXISTS_REPLACE); } diff --git a/src/PrintBuilderInterface.php b/src/PrintBuilderInterface.php index e6be8c1..fae2bef 100644 --- a/src/PrintBuilderInterface.php +++ b/src/PrintBuilderInterface.php @@ -55,14 +55,16 @@ interface PrintBuilderInterface { * The content entity to render. * @param \Drupal\entity_print\Plugin\PrintEngineInterface $print_engine * The plugin id of the Print engine to use. - * @param string $uri - * The URI on disk the file should be saved to. + * @param string $scheme + * The Drupal scheme. + * @param string $filename + * (optional) The filename or empty to have one generated. * @param bool $use_default_css * (optional) TRUE if you want the default CSS included, otherwise FALSE. * * @return string * FALSE or the URI to the file. E.g. public://my-file.pdf. */ - public function savePrintable(array $entities, PrintEngineInterface $print_engine, $uri, $use_default_css = TRUE); + public function savePrintable(array $entities, PrintEngineInterface $print_engine, $scheme = 'public', $filename = '', $use_default_css = TRUE); } diff --git a/tests/src/Kernel/PrintBuilderTest.php b/tests/src/Kernel/PrintBuilderTest.php index 863368a..b33ec45 100644 --- a/tests/src/Kernel/PrintBuilderTest.php +++ b/tests/src/Kernel/PrintBuilderTest.php @@ -120,9 +120,9 @@ class PrintBuilderTest extends KernelTestBase { $uri = $builder->savePrintable([$node], $print_engine); $this->assertRegExp('#public://(.*)\.pdf#', $uri); - $custom_uri = 'public://' . $this->randomMachineName() . 'pdf'; - $uri = $builder->savePrintable([$node], $print_engine, $custom_uri); - $this->assertEquals($custom_uri, $uri); + $filename = $this->randomMachineName() . 'pdf'; + $uri = $builder->savePrintable([$node], $print_engine, 'public', $filename); + $this->assertEquals("public://$filename", $uri); // Test the file contents. $this->assertEquals('Using testprintengine', file_get_contents($uri));