diff --git a/commerce_invoice.services.yml b/commerce_invoice.services.yml index b8b98c9..350c3ae 100644 --- a/commerce_invoice.services.yml +++ b/commerce_invoice.services.yml @@ -37,7 +37,7 @@ services: commerce_invoice.print_builder: class: Drupal\commerce_invoice\InvoicePrintBuilder - arguments: ['@config.factory', '@entity_type.manager', '@entity_print.print_builder', '@entity_print.filename_generator', '@current_user'] + arguments: ['@config.factory', '@entity_type.manager', '@entity_print.print_builder', '@entity_print.filename_generator', '@event_dispatcher', '@current_user'] commerce_invoice.route_subscriber: class: Drupal\commerce_invoice\EventSubscriber\RouteSubscriber diff --git a/src/Event/InvoiceEvents.php b/src/Event/InvoiceEvents.php index 2b4837a..d96193c 100644 --- a/src/Event/InvoiceEvents.php +++ b/src/Event/InvoiceEvents.php @@ -134,4 +134,13 @@ final class InvoiceEvents { */ const INVOICE_ITEM_DELETE = 'commerce_invoice.commerce_invoice_item.delete'; + /** + * Name of the event fired when generating an invoice filename. + * + * @Event + * + * @see \Drupal\commerce_invoice\Event\InvoiceFilenameEvent + */ + const INVOICE_FILENAME = 'commerce_invoice.filename'; + } diff --git a/src/Event/InvoiceFilenameEvent.php b/src/Event/InvoiceFilenameEvent.php new file mode 100644 index 0000000..cf27811 --- /dev/null +++ b/src/Event/InvoiceFilenameEvent.php @@ -0,0 +1,75 @@ +filename = $filename; + $this->invoice = $invoice; + } + + /** + * Gets the invoice filename. + * + * @return string + * The invoice filename. + */ + public function getFilename() { + return $this->filename; + } + + /** + * Sets the invoice filename. + * + * @param string $filename + * The invoice filename. + * + * @return $this + */ + public function setFilename($filename) { + $this->filename = $filename; + return $this; + } + + /** + * Gets the invoice. + * + * @return \Drupal\commerce_invoice\Entity\InvoiceInterface + * The invoice. + */ + public function getInvoice() { + return $this->invoice; + } + +} diff --git a/src/InvoicePrintBuilder.php b/src/InvoicePrintBuilder.php index 0531a2d..15f77de 100644 --- a/src/InvoicePrintBuilder.php +++ b/src/InvoicePrintBuilder.php @@ -3,12 +3,15 @@ namespace Drupal\commerce_invoice; use Drupal\commerce_invoice\Entity\InvoiceInterface; +use Drupal\commerce_invoice\Event\InvoiceEvents; +use Drupal\commerce_invoice\Event\InvoiceFilenameEvent; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Session\AccountInterface; use Drupal\entity_print\FilenameGeneratorInterface; use Drupal\entity_print\Plugin\PrintEngineInterface; use Drupal\entity_print\PrintBuilderInterface; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; /** * The print builder service. @@ -43,6 +46,13 @@ class InvoicePrintBuilder implements InvoicePrintBuilderInterface { */ protected $filenameGenerator; + /** + * The event dispatcher. + * + * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface + */ + protected $eventDispatcher; + /** * The Current User object. * @@ -61,14 +71,17 @@ class InvoicePrintBuilder implements InvoicePrintBuilderInterface { * The Entity print builder. * @param \Drupal\entity_print\FilenameGeneratorInterface $filename_generator * The Entity print filename generator. + * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher + * The event dispatcher. * @param \Drupal\Core\Session\AccountInterface $current_user * The current user. */ - public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, PrintBuilderInterface $print_builder, FilenameGeneratorInterface $filename_generator, AccountInterface $current_user) { + public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, PrintBuilderInterface $print_builder, FilenameGeneratorInterface $filename_generator, EventDispatcherInterface $event_dispatcher, AccountInterface $current_user) { $this->configFactory = $config_factory; $this->fileStorage = $entity_type_manager->getStorage('file'); $this->printBuilder = $print_builder; $this->filenameGenerator = $filename_generator; + $this->eventDispatcher = $event_dispatcher; $this->currentUser = $current_user; } @@ -76,9 +89,13 @@ class InvoicePrintBuilder implements InvoicePrintBuilderInterface { * {@inheritdoc} */ public function generateFilename(InvoiceInterface $invoice) { - $file_name = $this->filenameGenerator->generateFilename([$invoice]); - $file_name .= '-' . $invoice->language()->getId() . '-' . str_replace('_', '', $invoice->getState()->getId()) . '.pdf'; - return $file_name; + $filename = $this->filenameGenerator->generateFilename([$invoice]); + $filename .= '-' . $invoice->language()->getId() . '-' . str_replace('_', '', $invoice->getState()->getId()); + // Let the filename be altered. + $event = new InvoiceFilenameEvent($filename, $invoice); + $this->eventDispatcher->dispatch(InvoiceEvents::INVOICE_FILENAME, $event); + $filename = $event->getFilename() . '.pdf'; + return $filename; } /** diff --git a/tests/modules/commerce_invoice_test/commerce_invoice_test.services.yml b/tests/modules/commerce_invoice_test/commerce_invoice_test.services.yml index 0813b09..221b294 100644 --- a/tests/modules/commerce_invoice_test/commerce_invoice_test.services.yml +++ b/tests/modules/commerce_invoice_test/commerce_invoice_test.services.yml @@ -1,4 +1,9 @@ services: + commerce_invoice_test.invoice_filename_subscriber: + class: Drupal\commerce_invoice_test\EventSubscriber\InvoiceFilenameSubscriber + tags: + - { name: event_subscriber } + commerce_invoice_test.order_paid_subscriber: class: Drupal\commerce_invoice_test\EventSubscriber\InvoicePaidSubscriber tags: diff --git a/tests/modules/commerce_invoice_test/src/EventSubscriber/InvoiceFilenameSubscriber.php b/tests/modules/commerce_invoice_test/src/EventSubscriber/InvoiceFilenameSubscriber.php new file mode 100644 index 0000000..8632d10 --- /dev/null +++ b/tests/modules/commerce_invoice_test/src/EventSubscriber/InvoiceFilenameSubscriber.php @@ -0,0 +1,34 @@ + 'alterFilename', + ]; + } + + /** + * Alters the invoice filename. + * + * @param \Drupal\commerce_invoice\Event\InvoiceFilenameEvent $event + * The transition event. + */ + public function alterFilename(InvoiceFilenameEvent $event) { + $invoice = $event->getInvoice(); + // Alter the filename if the "alter_filename" data flag is present. + if ($invoice->getData('alter_filename')) { + $event->setFilename($event->getFilename() . '-altered'); + } + } + +} diff --git a/tests/src/Kernel/InvoicePrintBuilderTest.php b/tests/src/Kernel/InvoicePrintBuilderTest.php index 1601649..96ac3ae 100644 --- a/tests/src/Kernel/InvoicePrintBuilderTest.php +++ b/tests/src/Kernel/InvoicePrintBuilderTest.php @@ -112,6 +112,14 @@ class InvoicePrintBuilderTest extends InvoiceKernelTestBase { $this->assertRegExp('#public://(.*)\.pdf#', $file->getFileUri()); $this->assertEquals('10-en-paid.pdf', $file->getFilename()); $this->assertEquals('application/pdf', $file->getMimeType()); + + // Tests the filename alteration via an event subscriber. + $invoice->setData('alter_filename', TRUE); + $print_engine = $this->container->get('plugin.manager.entity_print.print_engine')->createInstance('testprintengine'); + $file = $this->printBuilder->savePrintable($invoice, $print_engine, 'public'); + $this->assertRegExp('#public://(.*)\.pdf#', $file->getFileUri()); + $this->assertEquals('10-en-paid-altered.pdf', $file->getFilename()); + $this->assertEquals('application/pdf', $file->getMimeType()); } }