From 620aa651aaaa34a39a46b774dab7196294adbac0 Mon Sep 17 00:00:00 2001 From: Will Rossiter Date: Mon, 5 Oct 2009 02:17:19 +0000 Subject: [PATCH] FEATURE: added extend() call to enable FieldHolder() html to be customized via extensions. git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@88104 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- forms/FormField.php | 17 +++++++++++++++++ tests/forms/FormFieldTest.php | 25 +++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/forms/FormField.php b/forms/FormField.php index 8ffec80b1..000d409c7 100755 --- a/forms/FormField.php +++ b/forms/FormField.php @@ -405,6 +405,23 @@ class FormField extends RequestHandler { // $MessageType is also used in {@link extraClass()} with a "holder-" prefix $messageBlock = (!empty($Message)) ? "$Message" : ""; + $values = array( + 'Title' => $Title, + 'Message' => $Message, + 'MessageType' => $MessageType, + 'RightTitle' => $RightTitle, + 'Type' => $Type, + 'extraClass' => $extraClass, + 'Name' => $Name, + 'Field' => $Field + ); + + // allow form field html to be decorated. + $subTemplates = $this->extend('updateFieldHolder', $values); + if($subTemplates && is_array($subTemplates)) { + return array_pop($subTemplates); + } + return <<$titleBlock
$Field
$rightTitleBlock$messageBlock HTML; diff --git a/tests/forms/FormFieldTest.php b/tests/forms/FormFieldTest.php index 02b58a558..f656550d7 100644 --- a/tests/forms/FormFieldTest.php +++ b/tests/forms/FormFieldTest.php @@ -85,5 +85,30 @@ class FormFieldTest extends SapphireTest { } } + /** + * Test to ensure that subclassing the form field FieldHolder updates the html + * the field produces + */ + function testCustomFieldOverridesTemplate() { + $orignal = new TextField('Name'); + $orignalHTML = $orignal->FieldHolder(); + + Object::add_extension('FormField', 'FormFieldTest_CustomFormSubclass'); + $textField = new TextField('Name'); + $newHTML = $textField->FieldHolder(); + + $this->assertNotSame($orignalHTML, $newHTML); + $this->assertSame($newHTML, '
  • '); + } +} + +class FormFieldTest_CustomFormSubclass extends Extension implements TestOnly { + + function updateFieldHolder(array $array) { + extract($array); + return << +HTML; + } } ?> \ No newline at end of file