EDIT: Since This article has been written the original code has taken down from our Subversion repository. to download a copy click here

In the course of my trying to do various things with the Zend Framework I am constantly haveing to create rich text editor fields.

Now the guys at Zend Framework have done a bang up job of of getting things started and the Dojo and jQuery libraries that are part of it are spectacular.. except that they dont offer an advanced text editor. Which I think is a bit of a shame. So I decided that In order to make my job a whole lot easier I would create a plugin that I could reuse as and when I needed it.

Please do bear in mind that this is not a completely finished plugin so feel free to fix the bits I miss. If you want to see/use my original code you can find it over at http://subversion.zucchi.co.uk.

So far This helper has allowed me to call the TinyMCE editor form countless views and forms.

View Helper

The first thing we need to set up is a view helper. This will actually allow us to access TinyMCE from anywhere in the Framework.

If you want to see all the code for my View helper you can grab it from here.

I’ll presume that you have your own library files set up. First we create a new helper calledt TinyMce.php containing a class

class MyLibrary_View_Helper_tinyMce extends Zend_View_Helper_Abstract {
}

You will then need to add the following variables & methods

public $view;
public function __construct() {}
public function tinyMce(){}
public function setView(Zend_View_Interface $view){}
public function __call($method, $args){}

The $view object will contain a copy of the Zend_View_Interface Object.

/**
     * Initialize helper
     *
     * Retrieve container from registry or create new container and store in
     * registry.
     *
     * @return void
     */
    public function __construct()
    {

        $registry = Zend_Registry::getInstance();
        if (!isset($registry[__CLASS__])) {
            require_once 'MyLibrary/View/Helper/TinyMce/Container.php';
            $container = new MyLibrary_View_Helper_TinyMce_Container();
            $registry[__CLASS__] = $container;
        }
        $this->_container = $registry[__CLASS__];
    }

The __construct as you can see referes to a “Container” class. We will cover this in the next page in more detail. Suffice to say that this is where most of the hard work will happen.

Here is the rest of the class which is pretty straight forward.

class MyLibrary_View_Helper_tinyMce extends Zend_View_Helper_Abstract
{

    public $view;

    public function __construct()
    {

        $registry = Zend_Registry::getInstance();
        if (!isset($registry[__CLASS__])) {
            require_once 'Zucchi/View/Helper/TinyMce/Container.php';
            $container = new Zucchi_View_Helper_TinyMce_Container();
            $registry[__CLASS__] = $container;
        }
        $this->_container = $registry[__CLASS__];
    }

    public function tinyMce()
    {
        return $this->_container;
    }

    public function setView(Zend_View_Interface $view)
    {
        $this->view = $view;
        $this->_container->setView($view);
    }

    public function __call($method, $args)
    {
        if (!method_exists($this->_container, $method)) {
            require_once 'Zend/View/Exception.php';
            throw new Zend_View_Exception(sprintf('Invalid method "%s" called on TinyMce view helper', $method));
        }

        return call_user_func_array(array($this->_container, $method), $args);
    }

}

Now onto the “Container” class that we mentioned earlier