ZF-6667: New "IMPLICIT" placement for Zend_Form_Decorator_Label

Description

Zend_Form_Decorator_Label has two placement modes: PREPEND and APPEND, described below:

PREPEND:


Name 

APPEND:


Name

IMPLICIT (currently not implemented):


Name 

Sample form using this technique: http://gaigalas.net/semanticforms.html

HTML reference for "implicit labels": http://w3.org/TR/html401/…

Comments

There is a problem:


Name 
 Name

Prepend or append? ;-)

My suggestion:


const IMPLICIT = 'IMPLICIT';
const IMPLICIT_PREPEND = 'IMPLICIT_PREPEND';

Name 

And


const IMPLICIT_APPEND = 'IMPLICIT_APPEND';

 Name

I think the priority "major" is greatly exaggerated!


$form = new Zend_Form();

$form->addElement(
    'text',
    'name',
    array(
        'label'       => 'Name',
        'description' => 'Workaround for implicit labels.',
        'decorators'  => array(
            'ViewHelper',
            array(
                // Decorator name
                'Callback',
                // Options
                array(
                    'callback' => function($content, $element, array $options)
                    {
                        // Get label
                        $label = $element->getLabel();

                        // Remove label from the element
                        $element->setLabel(null);

                        return $label;
                    },
                    'placement' => 'prepend',
                )
            ),
            array(
                // Decorator name
                array(
                    'label' => 'HtmlTag',
                ),
                // Options
                array(
                    'tag' => 'label',
                ),
            ),
            'Errors',
            'Description',
            array(
                // Decorator name
                array(
                    'dd' => 'HtmlTag',
                ),
                // Options
                array(
                    'tag' => 'dd',
                ),
            ),
            array(
                // Decorator name
                'Label',
                // Options
                array(
                    'tag' => 'dt',
                ),
            ),
        ),
    )
);

echo $form->render(new Zend_View());


    
  Name

Workaround for implicit labels.

Priority changed

Proposal:


class Zend_Form_Decorator_Label extends Zend_Form_Decorator_Abstract
{
    // ...
    
    const IMPLICIT         = 'IMPLICIT';
    const IMPLICIT_PREPEND = 'IMPLICIT_PREPEND';
    const IMPLICIT_APPEND  = 'IMPLICIT_APPEND';
    
    // ...
    
    public function getPlacement()
    {
        $placement = $this->_placement;
        if (null !== ($placementOpt = $this->getOption('placement'))) {
            $placementOpt = strtoupper($placementOpt);
            switch ($placementOpt) {
                case self::APPEND:
                case self::PREPEND:
                case self::IMPLICIT:
                case self::IMPLICIT_PREPEND:
                case self::IMPLICIT_APPEND:
                    $placement = $this->_placement = $placementOpt;
                    break;
                case false:
                    $placement = $this->_placement = null;
                    break;
                default:
                    break;
            }
            $this->removeOption('placement');
        }

        return $placement;
    }
    
    // ...
    
    public function render($content)
    {
        // ...
        
        if (!empty($label)) {
            $options['class'] = $class;
            
            $label = trim($label);
            
            switch ($placement) {
                case self::IMPLICIT:
                    // Break was intentionally omitted
                
                case self::IMPLICIT_PREPEND:
                    $options['escape']     = false;
                    $options['disableFor'] = true;
                    
                    $label = $view->formLabel(
                        $element->getFullyQualifiedName(),
                        $label . $separator . $content,
                        $options
                    );
                    break;
                    
                case self::IMPLICIT_APPEND:
                    $options['escape']     = false;
                    $options['disableFor'] = true;
                    
                    $label = $view->formLabel(
                        $element->getFullyQualifiedName(),
                        $content . $separator . $label,
                        $options
                    );
                    break;
                    
                case self::APPEND:
                    // Break was intentionally omitted
                
                case self::PREPEND:
                    // Break was intentionally omitted
                
                default:
                    $label = $view->formLabel(
                        $element->getFullyQualifiedName(),
                        $label,
                        $options
                    );
                    break;
            }
        } else {
            $label = ' ';
        }
        
        // ...
        
        switch ($placement) {
            case self::APPEND:
                return $content . $separator . $label;
                
            case self::PREPEND:
                return $label . $separator . $content;
                
            case self::IMPLICIT:
                // Break was intentionally omitted
                
            case self::IMPLICIT_PREPEND:
                // Break was intentionally omitted
                
            case self::IMPLICIT_APPEND:
                return $label;
        }
    }
}

$form = new Zend_Form();

$form->addElement(
    'text',
    'name',
    array(
        'label' => 'Name',
        'decorators' => array(
            'ViewHelper',
            'Errors',
            'Description',
            array(
                'Label',
                array(
                    'placement' => Zend_Form_Decorator_Label::IMPLICIT_APPEND,
                ),
            ),
            'DtDdWrapper',
        ),
    )
);

echo $form->render(new Zend_View());


    
  Name

Patch and unit tests added.

Fixed in trunk (1.12.): r24834