mirror of
https://github.com/silverstripe/silverstripe-userforms.git
synced 2024-10-22 17:05:42 +02:00
Merge pull request #594 from robbieaverill/bugfix/literal-field-is-flexible
FIX Make EditableLiteralField extensible, have its own template, honour visibility rules
This commit is contained in:
commit
2ef5643f39
@ -126,33 +126,32 @@ class EditableLiteralField extends EditableFormField
|
|||||||
|
|
||||||
public function getFormField()
|
public function getFormField()
|
||||||
{
|
{
|
||||||
// Build label and css classes
|
$content = LiteralField::create(
|
||||||
$label = '';
|
"LiteralFieldContent-{$this->ID}]",
|
||||||
$classes = $this->ExtraClass;
|
|
||||||
if (empty($this->Title) || $this->HideLabel) {
|
|
||||||
$classes .= " nolabel";
|
|
||||||
} else {
|
|
||||||
$label = "<label class='left'>{$this->EscapedTitle}</label>";
|
|
||||||
}
|
|
||||||
|
|
||||||
$field = new LiteralField(
|
|
||||||
"LiteralField[{$this->ID}]",
|
|
||||||
sprintf(
|
|
||||||
"<div id='%s' class='field text %s'>
|
|
||||||
%s
|
|
||||||
<div class='middleColumn literalFieldArea'>%s</div>".
|
|
||||||
"</div>",
|
|
||||||
Convert::raw2htmlname($this->Name),
|
|
||||||
Convert::raw2att($classes),
|
|
||||||
$label,
|
|
||||||
$this->dbObject('Content')->forTemplate()
|
$this->dbObject('Content')->forTemplate()
|
||||||
)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// When dealing with literal fields there is no further customisation that can be added at this point
|
$field = CompositeField::create($content)
|
||||||
|
->setName($this->Name)
|
||||||
|
->setID($this->Name)
|
||||||
|
->setFieldHolderTemplate('UserFormsLiteralField_holder');
|
||||||
|
|
||||||
|
$this->doUpdateFormField($field);
|
||||||
|
|
||||||
return $field;
|
return $field;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function updateFormField($field)
|
||||||
|
{
|
||||||
|
parent::updateFormField($field);
|
||||||
|
|
||||||
|
if ($this->HideLabel) {
|
||||||
|
$this->ExtraClass .= ' nolabel';
|
||||||
|
} else {
|
||||||
|
$field->setTitle($this->Title);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public function showInReports()
|
public function showInReports()
|
||||||
{
|
{
|
||||||
return ! $this->HideFromReports;
|
return ! $this->HideFromReports;
|
||||||
|
10
templates/forms/UserFormsLiteralField_holder.ss
Normal file
10
templates/forms/UserFormsLiteralField_holder.ss
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
<div id="$Name" class="field<% if $extraClass %> $extraClass<% end_if %>">
|
||||||
|
<% if $Title %><label class="left">$Title</label><% end_if %>
|
||||||
|
<div class="middleColumn">
|
||||||
|
<% loop $FieldList %>
|
||||||
|
$Field
|
||||||
|
<% end_loop %>
|
||||||
|
</div>
|
||||||
|
<% if $RightTitle %><span id="{$Name}_right_title" class="right-title">$RightTitle</span><% end_if %>
|
||||||
|
<% if $Message %><span class="message $MessageType">$Message</span><% end_if %>
|
||||||
|
</div>
|
@ -38,9 +38,39 @@ class EditableLiteralFieldTest extends SapphireTest
|
|||||||
'Title' => 'Test label'
|
'Title' => 'Test label'
|
||||||
));
|
));
|
||||||
|
|
||||||
$this->assertContains('Test label', $field->getFormField()->Field());
|
$this->assertContains('Test label', $field->getFormField()->FieldHolder());
|
||||||
|
$this->assertEquals('Test label', $field->getFormField()->Title());
|
||||||
|
|
||||||
$field->HideLabel = true;
|
$field->HideLabel = true;
|
||||||
$this->assertNotContains('Test label', $field->getFormField()->Field());
|
$this->assertNotContains('Test label', $field->getFormField()->FieldHolder());
|
||||||
|
$this->assertEmpty($field->getFormField()->Title());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testLiteralFieldHasUpdateFormFieldMethodCalled()
|
||||||
|
{
|
||||||
|
$field = $this->getMockBuilder('EditableLiteralField')
|
||||||
|
->setMethods(array('doUpdateFormField'))
|
||||||
|
->getMock();
|
||||||
|
|
||||||
|
$field->expects($this->once())->method('doUpdateFormField');
|
||||||
|
|
||||||
|
$field->getFormField();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* LiteralFields do not allow field names, etc. Instead, the field is contained within a composite field. This
|
||||||
|
* test ensures that this structure is correct.
|
||||||
|
*/
|
||||||
|
public function testLiteralFieldIsContainedWithinCompositeField()
|
||||||
|
{
|
||||||
|
$field = new EditableLiteralField;
|
||||||
|
$formField = $field->getFormField();
|
||||||
|
|
||||||
|
$this->assertInstanceOf('CompositeField', $formField, 'Literal field is contained within a composite field');
|
||||||
|
$this->assertInstanceOf(
|
||||||
|
'LiteralField',
|
||||||
|
$formField->FieldList()->first(),
|
||||||
|
'Actual literal field exists in composite field children'
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user