From e72e02888c483841af5d5d41d82335c9f8c40543 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Thu, 24 Jan 2013 10:52:52 -0500
Subject: [PATCH] Flashmessages view helper no longer relies on FlashMessenger
 object attached to the view -- instead it uses dependency injection through
 the plugin manager.

---
 .../src/VuFind/Controller/AbstractBase.php    |  3 -
 .../VuFind/View/Helper/Root/Flashmessages.php | 81 +++++++++++--------
 themes/root/theme.config.php                  |  6 +-
 3 files changed, 53 insertions(+), 37 deletions(-)

diff --git a/module/VuFind/src/VuFind/Controller/AbstractBase.php b/module/VuFind/src/VuFind/Controller/AbstractBase.php
index 4734427500e..7ac3151b317 100644
--- a/module/VuFind/src/VuFind/Controller/AbstractBase.php
+++ b/module/VuFind/src/VuFind/Controller/AbstractBase.php
@@ -69,9 +69,6 @@ class AbstractBase extends AbstractActionController
             }
         }
 
-        // Always make flash messenger available to view:
-        $view->flashMessenger = $this->flashMessenger();
-
         return $view;
     }
 
diff --git a/module/VuFind/src/VuFind/View/Helper/Root/Flashmessages.php b/module/VuFind/src/VuFind/View/Helper/Root/Flashmessages.php
index 75e7f5fab9e..1176d168909 100644
--- a/module/VuFind/src/VuFind/View/Helper/Root/Flashmessages.php
+++ b/module/VuFind/src/VuFind/View/Helper/Root/Flashmessages.php
@@ -26,7 +26,7 @@
  * @link     http://vufind.org/wiki/vufind2:developer_manual Wiki
  */
 namespace VuFind\View\Helper\Root;
-use Zend\View\Helper\AbstractHelper;
+use Zend\View\Helper\AbstractHelper, Zend\Mvc\Controller\Plugin\FlashMessenger;
 
 /**
  * Flash message view helper
@@ -39,6 +39,23 @@ use Zend\View\Helper\AbstractHelper;
  */
 class Flashmessages extends AbstractHelper
 {
+    /**
+     * Flash messenger controller helper
+     *
+     * @var FlashMessenger
+     */
+    protected $fm;
+
+    /**
+     * Constructor
+     *
+     * @param FlashMessenger $fm Flash messenger controller helper
+     */
+    public function __construct(FlashMessenger $fm)
+    {
+        $this->fm = $fm;
+    }
+
     /**
      * Generate flash message <div>'s with appropriate classes based on message type.
      *
@@ -47,42 +64,40 @@ class Flashmessages extends AbstractHelper
     public function __invoke()
     {
         $html = '';
-        if (is_object($fm = $this->getView()->flashMessenger)) {
-            $namespaces = array('error', 'info');
-            foreach ($namespaces as $ns) {
-                $fm->setNamespace($ns);
-                $messages = array_merge(
-                    $fm->getMessages(), $fm->getCurrentMessages()
-                );
-                foreach ($messages as $msg) {
-                    $html .= '<div class="' . $ns . '">';
-                    // Advanced form:
-                    if (is_array($msg)) {
-                        // Use a different translate helper depending on whether
-                        // or not we're in HTML mode.
-                        if (!isset($msg['translate']) || $msg['translate']) {
-                            $helper = (isset($msg['html']) && $msg['html'])
-                                ? 'translate' : 'transEsc';
-                        } else {
-                            $helper = (isset($msg['html']) && $msg['html'])
-                                ? false : 'escapeHtml';
-                        }
-                        $helper = $helper
-                            ? $this->getView()->plugin($helper) : false;
-                        $tokens = isset($msg['tokens']) ? $msg['tokens'] : array();
-                        $default = isset($msg['default']) ? $msg['default'] : null;
-                        $html .= $helper
-                            ? $helper($msg['msg'], $tokens, $default) : $msg['msg'];
+        $namespaces = array('error', 'info');
+        foreach ($namespaces as $ns) {
+            $this->fm->setNamespace($ns);
+            $messages = array_merge(
+                $this->fm->getMessages(), $this->fm->getCurrentMessages()
+            );
+            foreach ($messages as $msg) {
+                $html .= '<div class="' . $ns . '">';
+                // Advanced form:
+                if (is_array($msg)) {
+                    // Use a different translate helper depending on whether
+                    // or not we're in HTML mode.
+                    if (!isset($msg['translate']) || $msg['translate']) {
+                        $helper = (isset($msg['html']) && $msg['html'])
+                            ? 'translate' : 'transEsc';
                     } else {
-                        // Basic default string:
-                        $transEsc = $this->getView()->plugin('transEsc');
-                        $html .= $transEsc($msg);
+                        $helper = (isset($msg['html']) && $msg['html'])
+                            ? false : 'escapeHtml';
                     }
-                    $html .= '</div>';
+                    $helper = $helper
+                        ? $this->getView()->plugin($helper) : false;
+                    $tokens = isset($msg['tokens']) ? $msg['tokens'] : array();
+                    $default = isset($msg['default']) ? $msg['default'] : null;
+                    $html .= $helper
+                        ? $helper($msg['msg'], $tokens, $default) : $msg['msg'];
+                } else {
+                    // Basic default string:
+                    $transEsc = $this->getView()->plugin('transEsc');
+                    $html .= $transEsc($msg);
                 }
-                $fm->clearMessages();
-                $fm->clearCurrentMessages();
+                $html .= '</div>';
             }
+            $this->fm->clearMessages();
+            $this->fm->clearCurrentMessages();
         }
         return $html;
     }
diff --git a/themes/root/theme.config.php b/themes/root/theme.config.php
index b3385163dd7..a32c5ec7894 100644
--- a/themes/root/theme.config.php
+++ b/themes/root/theme.config.php
@@ -18,6 +18,11 @@ return array(
                     $sm->getServiceLocator()->get('VuFind\Translator')
                 );
             },
+            'flashmessages' => function ($sm) {
+                $messenger = $sm->getServiceLocator()->get('ControllerPluginManager')
+                    ->get('FlashMessenger');
+                return new \VuFind\View\Helper\Root\Flashmessages($messenger);
+            },
             'ils' => function ($sm) {
                 return new \VuFind\View\Helper\Root\Ils(
                     $sm->getServiceLocator()->get('VuFind\ILSConnection')
@@ -50,7 +55,6 @@ return array(
             'currentpath' => 'VuFind\View\Helper\Root\CurrentPath',
             'datetime' => 'VuFind\View\Helper\Root\DateTime',
             'excerpt' => 'VuFind\View\Helper\Root\Excerpt',
-            'flashmessages' => 'VuFind\View\Helper\Root\Flashmessages',
             'getlastsearchlink' => 'VuFind\View\Helper\Root\GetLastSearchLink',
             'highlight' => 'VuFind\View\Helper\Root\Highlight',
             'jqueryvalidation' => 'VuFind\View\Helper\Root\JqueryValidation',
-- 
GitLab