Prechádzať zdrojové kódy

Added support for setting a default attach order with headTitle view helper. Implements ZF-6718

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@22088 44c647ce-9c0f-0410-b52a-842ac1e357ba
padraic 15 rokov pred
rodič
commit
d8e92e88ca

+ 41 - 1
library/Zend/View/Helper/HeadTitle.php

@@ -54,6 +54,13 @@ class Zend_View_Helper_HeadTitle extends Zend_View_Helper_Placeholder_Container_
     protected $_translator;
 
     /**
+     * Default title rendering order (i.e. order in which each title attached)
+     *
+     * @var string
+     */
+    protected $_defaultAttachOrder = null;
+
+    /**
      * Retrieve placeholder for title element and optionally set state
      *
      * @param  string $title
@@ -61,8 +68,13 @@ class Zend_View_Helper_HeadTitle extends Zend_View_Helper_Placeholder_Container_
      * @param  string $separator
      * @return Zend_View_Helper_HeadTitle
      */
-    public function headTitle($title = null, $setType = Zend_View_Helper_Placeholder_Container_Abstract::APPEND)
+    public function headTitle($title = null, $setType = null)
     {
+        if (is_null($setType) && is_null($this->getDefaultAttachOrder())) {
+            $setType = Zend_View_Helper_Placeholder_Container_Abstract::APPEND;
+        } elseif (is_null($setType) && !is_null($this->getDefaultAttachOrder())) {
+            $setType = $this->getDefaultAttachOrder();
+        }
         $title = (string) $title;
         if ($title !== '') {
             if ($setType == Zend_View_Helper_Placeholder_Container_Abstract::SET) {
@@ -78,6 +90,34 @@ class Zend_View_Helper_HeadTitle extends Zend_View_Helper_Placeholder_Container_
     }
 
     /**
+     * Set a default order to add titles
+     *
+     * @param string $setType
+     */
+    public function setDefaultAttachOrder($setType)
+    {
+        if (!in_array($setType, array(
+            Zend_View_Helper_Placeholder_Container_Abstract::APPEND,
+            Zend_View_Helper_Placeholder_Container_Abstract::SET,
+            Zend_View_Helper_Placeholder_Container_Abstract::PREPEND
+        ))) {
+            require_once 'Zend/View/Exception.php';
+            throw new Zend_View_Exception("You must use a valid attach order: 'PREPEND', 'APPEND' or 'SET'");
+        }
+        $this->_defaultAttachOrder = $setType;
+    }
+
+    /**
+     * Get the default attach order, if any.
+     *
+     * @return mixed
+     */
+    public function getDefaultAttachOrder()
+    {
+        return $this->_defaultAttachOrder;
+    }
+
+    /**
      * Sets a translation Adapter for translation
      *
      * @param  Zend_Translate|Zend_Translate_Adapter $translate

+ 8 - 0
tests/Zend/View/Helper/HeadTitleTest.php

@@ -230,6 +230,14 @@ class Zend_View_Helper_HeadTitleTest extends PHPUnit_Framework_TestCase
         $this->helper->headTitle('0');
         $this->assertEquals('<title>0</title>', $this->helper->toString());
     }
+
+    public function testCanPrependTitlesUsingDefaultAttachOrder()
+    {
+        $this->helper->setDefaultAttachOrder('PREPEND');
+        $placeholder = $this->helper->headTitle('Foo');
+        $placeholder = $this->helper->headTitle('Bar');
+        $this->assertContains('BarFoo', $placeholder->toString());
+    }
 }
 
 // Call Zend_View_Helper_HeadTitleTest::main() if this source file is executed directly.