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
This commit is contained in:
Will Rossiter 2009-10-05 02:17:19 +00:00
parent aa127e52c9
commit 620aa651aa
2 changed files with 42 additions and 0 deletions

View File

@ -405,6 +405,23 @@ class FormField extends RequestHandler {
// $MessageType is also used in {@link extraClass()} with a "holder-" prefix
$messageBlock = (!empty($Message)) ? "<span class=\"message $MessageType\">$Message</span>" : "";
$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 <<<HTML
<div id="$Name" class="field $Type $extraClass">$titleBlock<div class="middleColumn">$Field</div>$rightTitleBlock$messageBlock</div>
HTML;

View File

@ -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, '<li id="Name"></li>');
}
}
class FormFieldTest_CustomFormSubclass extends Extension implements TestOnly {
function updateFieldHolder(array $array) {
extract($array);
return <<<HTML
<li id="$Name"></li>
HTML;
}
}
?>