ZF2 FlashMessenger view helper

Tags: , , , , ,

The FlashMessenger is a great controller plugin to use in your projects. First time I used it with ZF2 I was looking for a simple way of being able to access the messages from my layout and my view.

After some Googling I was surprised to see that there wasn't a solid view helper out there that did that. So I made this with the bonus of using Twitter Bootstrap classes and the option of retrieving the current messages as well.

File: module/Application/Module.php

class Module {

    /* ... */

    public function getViewHelperConfig()
    {
        return array(
            'factories' => array(
                'flashMessages' => function($sm) {
                    $flashmessenger = $sm->getServiceLocator()
                        ->get('ControllerPluginManager')
                        ->get('flashmessenger');

                    $messages = new \Application\View\Helper\FlashMessages();
                    $messages->setFlashMessenger($flashmessenger);

                    return $messages;
                }
            ),
        );
    }
}

File: module/Application/src/Application/View/Helper/FlashMessages.php

<?php

namespace Application\View\Helper;

use Zend\View\Helper\AbstractHelper;
use Zend\Mvc\Controller\Plugin\FlashMessenger as FlashMessenger;

/**
 * @author Rick <rick@thewebmen.com>
 * @company The Webmen
 */
class FlashMessages extends AbstractHelper
{
    /**
     * @var FlashMessenger
     */
    protected $flashMessenger;

    public function setFlashMessenger(FlashMessenger $flashMessenger)
    {
        $this->flashMessenger = $flashMessenger;
    }

    public function __invoke($includeCurrentMessages = false)
    {
        $messages = array(
            FlashMessenger::NAMESPACE_ERROR => array(),
            FlashMessenger::NAMESPACE_SUCCESS => array(),
            FlashMessenger::NAMESPACE_INFO => array(),
            FlashMessenger::NAMESPACE_DEFAULT => array()
        );

        foreach ($messages as $ns => &$m) {
            $m = $this->flashMessenger->getMessagesFromNamespace($ns);
            if ($includeCurrentMessages) {
                $m = array_merge($m, $this->flashMessenger->getCurrentMessagesFromNamespace($ns));
            }
        }

        return $messages;
    }
}

The usage is simple, from your layout or your view you can use this code to get the messages.

<?php foreach ($this->flashMessages() as $namespace => $messages) : ?>
    <?php if (count($messages)) : ?>
        <?php foreach ($messages as $message) : ?>
        <div class="alert alert-<?=$namespace?>">
            <?php echo $message; ?>
        </div>
        <?php endforeach; ?>
    <?php endif; ?>
<?php endforeach; ?>

You can also use $this->flashMessages(true) to include current messages (messages that are added on the same request).

<?php foreach ($this->flashMessages(true) as $namespace => $messages) : ?>

Read the full post | 6 comments

Magento string manipulation

Tags: , , , , , , ,

It sure came as a surprise to me that Magento had these pretty handy string manipulation function hidden into a helper named core/string.

Apart from having a lot of binary-safe and iconv functions that do the usual substr(), strlen(), etc. It also has powerful truncate(), str_split(), splitInjection() and splitWords() functions.

For your comfort I've made a little showcase of how these functions work an what their output is.

// truncate($string, $length = 80, $etc = '...', &$remainder = '', $breakWords = true)

$remainder = '';
$string = 'Vestibulum lobortis mattis massa. Fusce malesuada mauris -et purus interdum venenatis.';

echo Mage::helper('core/string')->truncate($string, 50, '...', $remainder, true);
// Vestibulum lobortis mattis massa. Fusce malesuada mauris et purus interdum venenatis. Aliquam er...

echo Mage::helper('core/string')->truncate($string, 50, '...', $remainder, false);
// Vestibulum lobortis mattis massa. Fusce malesuada mauris et purus interdum venenatis. Aliquam...

echo $remainder;
// malesuada mauris et purus interdum venenatis.

 

// splitInjection($str, $length = 50, $needle = '-', $insert = ' ')

$string = 'ABCDEFGHIKJLMNOPQRSTUVWXYZ';

echo Mage::helper('core/string')->splitInjection($string, 5, 'G');
// ABCDE FG HIKJLMNO PQRST UVWXY Z 

 

// splitWords($str, $uniqueOnly = false, $maxWordLength = 0, $wordSeparatorRegexp = '\s')

$string = 'I really really like turtles';

var_dump(Mage::helper('core/string')->splitWords($string));
/*
 * array(5) {
 *  [0]=>
 *  string(1) "I"
 *  [1]=>
 *  string(6) "really"
 *  [2]=>
 *  string(6) "really"
 *  [3]=>
 *  string(4) "like"
 *  [4]=>
 *  string(7) "turtles"
 *  }
 */

var_dump(Mage::helper('core/string')->splitWords($string, true));
/*
 * array(4) {
 *  ["I"]=>
 *  string(1) "I"
 *  ["really"]=>
 *  string(6) "really"
 *  ["like"]=>
 *  string(4) "like"
 *  ["turtles"]=>
 *  string(7) "turtles"
 *  }
 */

Read the full post | 0 comments