From dbe809b3f8e4b3ca95fecc08f777f0d66f768f47 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Sun, 9 Mar 2014 21:22:03 -0400 Subject: [PATCH] Add a way to reset templates on the FormHelper. Now that there are multiple ways to mutate the templates at runtime, having a way to restore any edits will probably come in handy. --- src/View/Helper/FormHelper.php | 13 +++++++++++++ tests/TestCase/View/Helper/FormHelperTest.php | 13 +++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/View/Helper/FormHelper.php b/src/View/Helper/FormHelper.php index 97e15fe8fa5..835d1f28c7e 100755 --- a/src/View/Helper/FormHelper.php +++ b/src/View/Helper/FormHelper.php @@ -2232,6 +2232,19 @@ public function widget($name, array $data = []) { return $this->_registry->get($name)->render($data); } +/** + * Restores the default values built into FormHelper. + * + * This method will not reset any templates set in custom widgets. + * + * @return void + */ + public function resetTemplates() { + $reflection = new \ReflectionClass($this); + $properties = $reflection->getDefaultProperties(); + $this->templates($properties['_defaultTemplates']); + } + /** * Event listeners. * diff --git a/tests/TestCase/View/Helper/FormHelperTest.php b/tests/TestCase/View/Helper/FormHelperTest.php index 6b0dd5aefa7..204d9b8ff41 100755 --- a/tests/TestCase/View/Helper/FormHelperTest.php +++ b/tests/TestCase/View/Helper/FormHelperTest.php @@ -5770,4 +5770,17 @@ public function testNestInputInLabel() { $this->assertTags($result, $expected); } +/** + * Test resetting templates. + * + * @return void + */ + public function testResetTemplates() { + $this->Form->templates(['input' => '']); + $this->assertEquals('', $this->Form->getTemplater()->get('input')); + + $this->assertNull($this->Form->resetTemplates()); + $this->assertNotEquals('', $this->Form->getTemplater()->get('input')); + } + }