PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Plugin Loaders

Zend_Form_Element makes use of Zend_Loader_PluginLoader to allow developers to specify locations of alternate validators, filters, and decorators. Each has its own plugin loader associated with it, and general accessors are used to retrieve and modify each.

The following loader types are used with the various plugin loader methods: 'validate', 'filter', and 'decorator'. The type names are case insensitive.

The methods used to interact with plugin loaders are as follows:

  • setPluginLoader($loader, $type): $loader is the plugin loader object itself, while $type is one of the types specified above. This sets the plugin loader for the given type to the newly specified loader object.

  • getPluginLoader($type): retrieves the plugin loader associated with $type.

  • addPrefixPath($prefix, $path, $type = null): adds a prefix/path association to the loader specified by $type. If $type is NULL, it will attempt to add the path to all loaders, by appending the prefix with each of "_Validate", "_Filter", and "_Decorator"; and appending the path with "Validate/", "Filter/", and "Decorator/". If you have all your extra form element classes under a common hierarchy, this is a convenience method for setting the base prefix for them.

  • addPrefixPaths(array $spec): allows you to add many paths at once to one or more plugin loaders. It expects each array item to be an array with the keys 'path', 'prefix', and 'type'.

Custom validators, filters, and decorators are an easy way to share functionality between forms and to encapsulate custom functionality.

Example 441. Custom Label

One common use case for plugins is to provide replacements for standard classes. For instance, if you want to provide a different implementation of the 'Label' decorator -- for instance, to always append a colon -- you could create your own 'Label' decorator with your own class prefix, and then add it to your prefix path.

Let's start with a custom Label decorator. We'll give it the class prefix "My_Decorator", and the class itself will be in the file "My/Decorator/Label.php".

<?php
class My_Decorator_Label extends Zend_Form_Decorator_Abstract
{
    protected 
$_placement 'PREPEND';

    public function 
render($content)
    {
        if (
null === ($element $this->getElement())) {
            return 
$content;
        }
        if (!
method_exists($element'getLabel')) {
            return 
$content;
        }

        
$label $element->getLabel() . ':';

        if (
null === ($view $element->getView())) {
            return 
$this->renderLabel($content$label);
        }

        
$label $view->formLabel($element->getName(), $label);

        return 
$this->renderLabel($content$label);
    }

    public function 
renderLabel($content$label)
    {
        
$placement $this->getPlacement();
        
$separator $this->getSeparator();

        switch (
$placement) {
            case 
'APPEND':
                return 
$content $separator $label;
            case 
'PREPEND':
            default:
                return 
$label $separator $content;
        }
    }
}

Now we can tell the element to use this plugin path when looking for decorators:

<?php
$element
->addPrefixPath('My_Decorator''My/Decorator/''decorator');

Alternately, we can do that at the form level to ensure all decorators use this path:

<?php
$form
->addElementPrefixPath('My_Decorator''My/Decorator/''decorator');

After it added as in the example above, the 'My/Decorator/' path will be searched first to see if the decorator exists there when you add a decorator. As a result, 'My_Decorator_Label' will now be used when the 'Label' decorator is requested.


Zend Framework