Преглед изворни кода

ZF-6667: Allow implicit placement of labels with Zend_Form_Decorator_Label

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@24834 44c647ce-9c0f-0410-b52a-842ac1e357ba
adamlundrigan пре 13 година
родитељ
комит
ba222026cc

+ 34 - 3
documentation/manual/en/module_specs/Zend_Form-StandardDecorators.xml

@@ -390,10 +390,41 @@ class Util
 
         <para>
             By default, the Label decorator prepends to the provided
-            content; specify a 'placement' option of 'append' to place it
-            after the content.
+            content; This can be controlled by specifying one of the following
+            'placement' options:
         </para>
-    </sect2>
+
+        <itemizedlist>
+            <listitem>
+                <para>
+                    <property>PREPEND</property>: render the label before the
+                    content.
+                </para>
+            </listitem>
+
+            <listitem>
+                <para>
+                    <property>APPEND</property>: render the label after the
+                    content.
+                </para>
+            </listitem>
+
+            <listitem>
+                <para>
+                    <property>IMPLICIT_PREPEND</property>: render the element
+                    inside the label tag, placing the label text before the content.
+                </para>
+            </listitem>
+
+            <listitem>
+                <para>
+                    <property>IMPLICIT_APPEND</property>: render the element
+                    inside the label tag, placing the label text after the content.
+                </para>
+            </listitem>
+        </itemizedlist>
+
+   </sect2>
 
     <sect2 id="zend.form.standardDecorators.prepareElements">
         <title>Zend_Form_Decorator_PrepareElements</title>

+ 88 - 1
library/Zend/Form/Decorator/Label.php

@@ -46,6 +46,13 @@ require_once 'Zend/Form/Decorator/Abstract.php';
 class Zend_Form_Decorator_Label extends Zend_Form_Decorator_Abstract
 {
     /**
+     * Placement constants
+     */
+    const IMPLICIT         = 'IMPLICIT';
+    const IMPLICIT_PREPEND = 'IMPLICIT_PREPEND';
+    const IMPLICIT_APPEND  = 'IMPLICIT_APPEND';
+
+    /**
      * Default placement: prepend
      * @var string
      */
@@ -321,6 +328,35 @@ class Zend_Form_Decorator_Label extends Zend_Form_Decorator_Abstract
         return $label;
     }
 
+    /**
+     * Determine if label should append, prepend or implicit content
+     *
+     * @return string
+     */
+    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;
+    }
 
     /**
      * Render a label
@@ -352,7 +388,48 @@ class Zend_Form_Decorator_Label extends Zend_Form_Decorator_Abstract
 
         if (!empty($label)) {
             $options['class'] = $class;
-            $label = $view->formLabel($element->getFullyQualifiedName(), trim($label), $options);
+            $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 = '&#160;';
         }
@@ -375,8 +452,18 @@ class Zend_Form_Decorator_Label extends Zend_Form_Decorator_Abstract
         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;
         }
     }
 }

+ 75 - 0
tests/Zend/Form/Decorator/LabelTest.php

@@ -323,6 +323,81 @@ class Zend_Form_Decorator_LabelTest extends PHPUnit_Framework_TestCase
         $tagClass = $this->decorator->getTagClass();
         $this->assertTrue( NULL === $tagClass, $tagClass );
     }
+
+    /**
+     * @group ZF-6667
+     */
+    public function testRenderImplicitsOnRequest()
+    {
+        $element = new Zend_Form_Element('foo');
+        $element->setView($this->getView())
+                ->setLabel('My Label');
+
+        $this->decorator->setElement($element)
+                        ->setOptions(
+                            array(
+                                'placement' => 'IMPLICIT',
+                                'separator' => ' ',
+                            )
+                        );
+
+        $content = 'test content';
+        $actual  = $this->decorator->render($content);
+
+        $expected = '<label class="optional">My Label test content</label>';
+
+        $this->assertEquals($expected, $actual);
+    }
+
+    /**
+     * @group ZF-6667
+     */
+    public function testRenderImplicitPrependsOnRequest()
+    {
+        $element = new Zend_Form_Element('foo');
+        $element->setView($this->getView())
+                ->setLabel('My Label');
+
+        $this->decorator->setElement($element)
+                        ->setOptions(
+                            array(
+                                'placement' => 'IMPLICIT_PREPEND',
+                                'separator' => ' ',
+                            )
+                        );
+
+        $content = 'test content';
+        $actual  = $this->decorator->render($content);
+
+        $expected = '<label class="optional">My Label test content</label>';
+
+        $this->assertEquals($expected, $actual);
+    }
+
+    /**
+     * @group ZF-6667
+     */
+    public function testRenderImplicitAppendsOnRequest()
+    {
+        $element = new Zend_Form_Element('foo');
+        $element->setView($this->getView())
+                ->setLabel('My Label');
+
+        $this->decorator->setElement($element)
+                        ->setOptions(
+                            array(
+                                'placement' => 'IMPLICIT_APPEND',
+                                'separator' => ' ',
+                            )
+                        );
+
+        $content = 'test content';
+        $actual  = $this->decorator->render($content);
+
+        $expected = '<label class="optional">test content My Label</label>';
+
+        $this->assertEquals($expected, $actual);
+    }
 }
 
 // Call Zend_Form_Decorator_LabelTest::main() if this source file is executed directly.