From a1e31c7219db04f55e42673813624c4217c6973e Mon Sep 17 00:00:00 2001
From: Kevin Kaland <kevin@wizonesolutions.com>
Date: Sun, 8 Nov 2015 22:53:06 +0100
Subject: [PATCH] Issue #2359213: Implement redirect action.

---
 src/Controller/HandlePdfController.php        |  1 -
 src/OutputHandlerInterface.php                |  4 +--
 .../FillPdfRedirectAction.php                 | 36 +++++++++++++++++++
 .../FillPdfActionPlugin/FillPdfSaveAction.php |  8 +++--
 4 files changed, 44 insertions(+), 5 deletions(-)
 create mode 100644 src/Plugin/FillPdfActionPlugin/FillPdfRedirectAction.php

diff --git a/src/Controller/HandlePdfController.php b/src/Controller/HandlePdfController.php
index 406aead..7a41c6d 100644
--- a/src/Controller/HandlePdfController.php
+++ b/src/Controller/HandlePdfController.php
@@ -193,7 +193,6 @@ class HandlePdfController extends ControllerBase {
     $output_name = $this->buildFilename($fillpdf_form->title->value, $token_objects);
 
     // Determine the appropriate action for the PDF.
-    // @todo: If they checked destination_redirect, make it redirect
     $destination_path_set = !empty($fillpdf_form->destination_path->value);
     $redirect = !empty($fillpdf_form->destination_redirect->value);
     if ($destination_path_set && !$redirect) {
diff --git a/src/OutputHandlerInterface.php b/src/OutputHandlerInterface.php
index 86cf837..6c2799a 100644
--- a/src/OutputHandlerInterface.php
+++ b/src/OutputHandlerInterface.php
@@ -6,7 +6,7 @@
  */
 
 namespace Drupal\fillpdf;
-use Drupal\file\Entity\File;
+use Drupal\file\FileInterface;
 
 /**
  * Contains functions to standardize output handling for generated PDFs.
@@ -27,7 +27,7 @@ interface OutputHandlerInterface {
    *       the PDF should be presented.
    *
    * @param string $destination_path_override
-   * @return bool|\Drupal\file\Entity\File
+   * @return bool|FileInterface
    */
   public function savePdfToFile(array $context, $destination_path_override = NULL);
 
diff --git a/src/Plugin/FillPdfActionPlugin/FillPdfRedirectAction.php b/src/Plugin/FillPdfActionPlugin/FillPdfRedirectAction.php
new file mode 100644
index 0000000..8db2937
--- /dev/null
+++ b/src/Plugin/FillPdfActionPlugin/FillPdfRedirectAction.php
@@ -0,0 +1,36 @@
+<?php
+/**
+ * @file
+ * Contains \Drupal\fillpdf\Plugin\FillPdfActionPlugin\FillPdfRedirectAction.
+ */
+
+namespace Drupal\fillpdf\Plugin\FillPdfActionPlugin;
+
+use Drupal\Core\Annotation\Translation;
+use Drupal\fillpdf\Annotation\FillPdfActionPlugin;
+use Drupal\fillpdf\OutputHandler;
+use Drupal\fillpdf\Plugin\FillPdfActionPluginBase;
+use Symfony\Component\DependencyInjection\ContainerInterface;
+use Symfony\Component\HttpFoundation\RedirectResponse;
+
+/**
+ * Class FillPdfRedirectAction
+ * @package Drupal\fillpdf\Plugin\FillPdfActionPlugin
+ *
+ * @FillPdfActionPlugin(
+ *   id = "redirect",
+ *   label = @Translation("Redirect PDF to file")
+ * )
+ */
+class FillPdfRedirectAction extends FillPdfSaveAction {
+
+  public function execute() {
+    $saved_file = $this->savePdf();
+
+    // Get file URI, then return a RedirectResponse to it.
+    $destination = $saved_file->url();
+
+    return new RedirectResponse($destination);
+  }
+
+}
diff --git a/src/Plugin/FillPdfActionPlugin/FillPdfSaveAction.php b/src/Plugin/FillPdfActionPlugin/FillPdfSaveAction.php
index 3c17893..ba8fd31 100644
--- a/src/Plugin/FillPdfActionPlugin/FillPdfSaveAction.php
+++ b/src/Plugin/FillPdfActionPlugin/FillPdfSaveAction.php
@@ -38,12 +38,16 @@ class FillPdfSaveAction extends FillPdfActionPluginBase {
   }
 
   public function execute() {
-    // @todo: Error handling?
-    $this->outputHandler->savePdfToFile($this->configuration);
+    $this->savePdf();
 
     // @todo: Fix based on value of post_save_redirect, once I add that
     $response = new RedirectResponse('/');
     return $response;
   }
 
+  protected function savePdf() {
+    // @todo: Error handling?
+    return $this->outputHandler->savePdfToFile($this->configuration);
+  }
+
 }
-- 
GitLab