Search code examples
zend-frameworkhelperbootstrappingview-helperszend-view

How to disable some Zend View Helpers


I'm trying to make a way to disable some view helpers that are inside "application/views/helpers"...

What I really want is to put some options on the application.ini to enable or disable some Helpers.

Example on application.ini:

helpers.Helper1=on
helpers.Helper2=off

Now the problem is that when a Helper is off, I want to rewrite some functions of this helper in order to return a different result on the view. In this way, I don't need to change anything in the view script.

I thought in having 2 different php files for each helper, in different locations. One with the real helper and another with the changed helper (to work when it is off on the application.ini).

The problem is that I don't know how to tell the view which one it shoul load...

Does anyone know how it could be done?

FINAL CODE

Ok, after many tries, I put it to work with the following code:

Bootstrap

protected function _initConfigureHelpers(){
    $this->bootstrap('view');
    $view = $this->getResource('view');

    $view->addHelperPath("./../library/ConfigHelpers","Configurable_Helper");
    $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
        'ViewRenderer'
    );
    $viewRenderer->setView($view);
    $front  = Zend_Controller_Front::getInstance();
    $front->registerPlugin(new Application_Plugin_ViewPlugins());
    return $view;
}

Application_Plugin_ViewPlugins

class Application_Plugin_ViewPlugins extends Zend_Controller_Plugin_Abstract
{

    public function preDispatch(Zend_Controller_Request_Abstract $request){

        $front=Zend_Controller_Front::getInstance();
        $bootstrap=$front->getParam('bootstrap');
        $options=$bootstrap->getOption("helpers");
        if (is_array($options)){
            $view = $bootstrap->getResource('view');

            foreach($options as $option => $value){
                $helper=$view->getHelper($option);
                if ($helper){
                    if ($value=="off")
                        $helper->__disable();
                    else if ($value!="on")
                        throw new Exception('The value of helpers.'.$option.' must be "on" or "off" on application.ini.');
                } else {
                    throw new Exception("Inexistent Helper");
                }
            }
        }
    }

}

Modified helper example

require_once APPLICATION_HELPERS."CssCrush.php";

class Configurable_Helper_CssCrush extends Zend_View_Helper_CssCrush {

    protected $__config_enabled = true;

    public function __disable(){
        $this->__config_enabled = false;
        return $this;
    }


    public function __enable(){
        $this->__config_enabled = true;
        return $this;
    }

    public function cssCrush(){
        if ($this->__config_enabled){
            return parent::cssCrush();
        } else{
            return new Modified_CssCrush();
        }
    }

}

class Modified_CssCrush {

    public static function file ( $file, $options = null ) {
        return $file;
    }

}

APPLICATION_HELPERS is defined on /public/index.php as "../application/views/helpers/".

Now, when I want to add a configurable helper, I put the original helper on "/application/views/helpers/" and then, create a modified version of it on "/library/ConfigHelpers" with the structure of the example above.


Solution

  • What I think you want is Dependency Injection which is coming in zf2, but not available in zf1.

    With some tinkering though you can get what you need.

    Configuring helpers in the bootstrap

    (assumes default project structure)

    View helpers paths config : application/configs/application.ini:

    resources.view.helperPath.Zf_View_Helper_ = "Zf/View/Helper"
    

    A simple configurable helper, (allows disable/enable but you can obviously add any methods you need (use this as base class for helpers that need the behaviour)

    class Zf_View_Helper_Configurable extends Zend_View_Helper_Abstract
    {
        protected $isEnabled = true;
    
        public function configurable()
        {
            return $this;
        }
    
        public function disable()
        {
            $this->isEnabled = false;
            return $this;
        }
    
    
        public function enable()
        {
            $this->isEnabled = true;
            return $this;
        }
    
        public function __toString()
        {
            if ($this->isEnabled) {
                return 'Configurable is enabled';
            } else {
                return 'Configurable is disabled';
            }
        }
    }
    

    And configure the helpers in the bootstrap:

    public function _initConfigureHelpers()
    {
        $this->bootstrap('view');
        $view = $this->getResource('view');
    
        $configurableHelper = $view->configurable();
        $configurableHelper->disable();
    }
    

    You can add options in the .ini file and grab them in the bootstrap initConfigureHelpers() method.

    If you want this behaviour from any default zf helper, do what @Ratzo said and extend those helpers and add the required behaviour and then configure them in your bootstrap.