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:
Franco Springveldt 2017-05-18 23:10:09 +12:00 committed by GitHub
commit 2ef5643f39
3 changed files with 63 additions and 24 deletions

View File

@ -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; $this->dbObject('Content')->forTemplate()
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()
)
); );
// 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;

View 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>

View File

@ -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'
);
} }
} }