Browse Source

Merge pull request #15158 from cakephp/4.next-add-helper-options

Allow passing options for helpers.
Mark Story 5 years ago
parent
commit
1e0c0d36bb
2 changed files with 22 additions and 2 deletions
  1. 9 2
      src/View/ViewBuilder.php
  2. 13 0
      tests/TestCase/View/ViewBuilderTest.php

+ 9 - 2
src/View/ViewBuilder.php

@@ -305,12 +305,19 @@ class ViewBuilder implements JsonSerializable, Serializable
      * Adds a helper to use.
      *
      * @param string $helper Helper to use.
+     * @param array $options Options.
      * @return $this
      * @since 4.1.0
      */
-    public function addHelper(string $helper)
+    public function addHelper(string $helper, array $options = [])
     {
-        return $this->setHelpers([$helper]);
+        if ($options) {
+            $array = [$helper => $options];
+        } else {
+            $array = [$helper];
+        }
+
+        return $this->setHelpers($array);
     }
 
     /**

+ 13 - 0
tests/TestCase/View/ViewBuilderTest.php

@@ -448,4 +448,17 @@ class ViewBuilderTest extends TestCase
         ];
         $this->assertSame($expected, $helpers);
     }
+
+    /**
+     * @return void
+     */
+    public function testAddHelperOptions()
+    {
+        $builder = new ViewBuilder();
+        $builder->addHelper('Form')
+            ->addHelper('Text', ['foo' => 'bar']);
+
+        $helpers = $builder->getHelpers();
+        $this->assertSame(['foo' => 'bar'], $helpers['Text']);
+    }
 }