Skip to content
Snippets Groups Projects
Commit e72e0288 authored by Demian Katz's avatar Demian Katz
Browse files

Flashmessages view helper no longer relies on FlashMessenger object attached...

Flashmessages view helper no longer relies on FlashMessenger object attached to the view -- instead it uses dependency injection through the plugin manager.
parent b42b5808
No related merge requests found
...@@ -69,9 +69,6 @@ class AbstractBase extends AbstractActionController ...@@ -69,9 +69,6 @@ class AbstractBase extends AbstractActionController
} }
} }
// Always make flash messenger available to view:
$view->flashMessenger = $this->flashMessenger();
return $view; return $view;
} }
......
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
* @link http://vufind.org/wiki/vufind2:developer_manual Wiki * @link http://vufind.org/wiki/vufind2:developer_manual Wiki
*/ */
namespace VuFind\View\Helper\Root; namespace VuFind\View\Helper\Root;
use Zend\View\Helper\AbstractHelper; use Zend\View\Helper\AbstractHelper, Zend\Mvc\Controller\Plugin\FlashMessenger;
/** /**
* Flash message view helper * Flash message view helper
...@@ -39,6 +39,23 @@ use Zend\View\Helper\AbstractHelper; ...@@ -39,6 +39,23 @@ use Zend\View\Helper\AbstractHelper;
*/ */
class Flashmessages extends 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. * Generate flash message <div>'s with appropriate classes based on message type.
* *
...@@ -47,42 +64,40 @@ class Flashmessages extends AbstractHelper ...@@ -47,42 +64,40 @@ class Flashmessages extends AbstractHelper
public function __invoke() public function __invoke()
{ {
$html = ''; $html = '';
if (is_object($fm = $this->getView()->flashMessenger)) { $namespaces = array('error', 'info');
$namespaces = array('error', 'info'); foreach ($namespaces as $ns) {
foreach ($namespaces as $ns) { $this->fm->setNamespace($ns);
$fm->setNamespace($ns); $messages = array_merge(
$messages = array_merge( $this->fm->getMessages(), $this->fm->getCurrentMessages()
$fm->getMessages(), $fm->getCurrentMessages() );
); foreach ($messages as $msg) {
foreach ($messages as $msg) { $html .= '<div class="' . $ns . '">';
$html .= '<div class="' . $ns . '">'; // Advanced form:
// Advanced form: if (is_array($msg)) {
if (is_array($msg)) { // Use a different translate helper depending on whether
// Use a different translate helper depending on whether // or not we're in HTML mode.
// or not we're in HTML mode. if (!isset($msg['translate']) || $msg['translate']) {
if (!isset($msg['translate']) || $msg['translate']) { $helper = (isset($msg['html']) && $msg['html'])
$helper = (isset($msg['html']) && $msg['html']) ? 'translate' : 'transEsc';
? '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'];
} else { } else {
// Basic default string: $helper = (isset($msg['html']) && $msg['html'])
$transEsc = $this->getView()->plugin('transEsc'); ? false : 'escapeHtml';
$html .= $transEsc($msg);
} }
$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(); $html .= '</div>';
$fm->clearCurrentMessages();
} }
$this->fm->clearMessages();
$this->fm->clearCurrentMessages();
} }
return $html; return $html;
} }
......
...@@ -18,6 +18,11 @@ return array( ...@@ -18,6 +18,11 @@ return array(
$sm->getServiceLocator()->get('VuFind\Translator') $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) { 'ils' => function ($sm) {
return new \VuFind\View\Helper\Root\Ils( return new \VuFind\View\Helper\Root\Ils(
$sm->getServiceLocator()->get('VuFind\ILSConnection') $sm->getServiceLocator()->get('VuFind\ILSConnection')
...@@ -50,7 +55,6 @@ return array( ...@@ -50,7 +55,6 @@ return array(
'currentpath' => 'VuFind\View\Helper\Root\CurrentPath', 'currentpath' => 'VuFind\View\Helper\Root\CurrentPath',
'datetime' => 'VuFind\View\Helper\Root\DateTime', 'datetime' => 'VuFind\View\Helper\Root\DateTime',
'excerpt' => 'VuFind\View\Helper\Root\Excerpt', 'excerpt' => 'VuFind\View\Helper\Root\Excerpt',
'flashmessages' => 'VuFind\View\Helper\Root\Flashmessages',
'getlastsearchlink' => 'VuFind\View\Helper\Root\GetLastSearchLink', 'getlastsearchlink' => 'VuFind\View\Helper\Root\GetLastSearchLink',
'highlight' => 'VuFind\View\Helper\Root\Highlight', 'highlight' => 'VuFind\View\Helper\Root\Highlight',
'jqueryvalidation' => 'VuFind\View\Helper\Root\JqueryValidation', 'jqueryvalidation' => 'VuFind\View\Helper\Root\JqueryValidation',
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment