ZF-3332: Fix HtmlTag Decorator to Accept Tag Content

Description

If the HtmlTag decorator allowed for a 'markup' option, then you could add HTML markup by passing a 'markup' option:


    $element = $this->getElement("password");
    $element->addDecorator(array(
        'GeneralError' => 'HtmlTag'),
        array('tag' => 'div', 'class' => 'errors',
        'placement' => "prepend",
        'markup' => $this->getTranslator()->translate('login.form.loginfailed')));

HtmlTag would require the following simple changes (I've marked them with a trailing comment):


    /**
     * Render content wrapped in an HTML tag
     *
     * @param  string $content
     * @return string
     */
    public function render($content)
    {
        $tag       = $this->getTag();
        $placement = $this->getPlacement();
        $noAttribs = $this->getOption('noAttribs');
        $openOnly  = $this->getOption('openOnly');
        $closeOnly = $this->getOption('closeOnly');
        $markup = $this->getOption('markup'); // html string to place after the opening tag -BF
        $this->removeOption('noAttribs');
        $this->removeOption('openOnly');
        $this->removeOption('closeOnly');
        $this->removeOption('markup');         // remove, if supplied -BF
       
        $attribs = null;
        if (!$noAttribs) {
            $attribs = $this->getOptions();
        }

        switch ($placement) {
            case self::APPEND:
                if ($closeOnly) {
                    return $content . $markup . $this->_getCloseTag($tag); // added ". $markup" -BF
                }
                if ($openOnly) {
                    return $content . $this->_getOpenTag($tag, $attribs)
                    . $markup;          // added ". $markup" -BF
                }
                return $content
                     . $this->_getOpenTag($tag, $attribs)
                     . $markup        // added ". $markup" -BF
                     . $this->_getCloseTag($tag);
            case self::PREPEND:
                if ($closeOnly) {
                    return  $markup . $this->_getCloseTag($tag) . $content; // added $markup -BF
                }
                if ($openOnly) {
                    return $this->_getOpenTag($tag, $attribs) . $markup . $content; // added $markup -BF
                }
                return $this->_getOpenTag($tag, $attribs)
                     . $markup           // added ". $markup" -BF
                     . $this->_getCloseTag($tag)
                     . $content;
            default:
                return (($openOnly || !$closeOnly) ? $this->_getOpenTag($tag, $attribs) : '')
                     . $content
                     . (($closeOnly || !$openOnly) ? $this->_getCloseTag($tag) : '');
        }
    }
} 

Comments

Could you please create a patch from the file you are changing using the version from the trunk?

Scheduling for next minor release.

This would be a vaulable addition. As discussed on #zftalk this would allow to easily inject headers and other header related content into Zend_Form.