From 11f43c27dc56f0e592ad63ea15fab951d5be61bb Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Thu, 18 May 2017 10:31:18 +1200 Subject: [PATCH] FIX Make EditableLiteralField extensible, have its own template, honour visibility rules --- .../EditableLiteralField.php | 43 +++++++++---------- .../forms/UserFormsLiteralField_holder.ss | 10 +++++ tests/EditableLiteralFieldTest.php | 34 ++++++++++++++- 3 files changed, 63 insertions(+), 24 deletions(-) create mode 100644 templates/forms/UserFormsLiteralField_holder.ss diff --git a/code/model/editableformfields/EditableLiteralField.php b/code/model/editableformfields/EditableLiteralField.php index 2c80277..9f9e271 100644 --- a/code/model/editableformfields/EditableLiteralField.php +++ b/code/model/editableformfields/EditableLiteralField.php @@ -126,33 +126,32 @@ class EditableLiteralField extends EditableFormField public function getFormField() { - // Build label and css classes - $label = ''; - $classes = $this->ExtraClass; - if (empty($this->Title) || $this->HideLabel) { - $classes .= " nolabel"; - } else { - $label = ""; - } - - $field = new LiteralField( - "LiteralField[{$this->ID}]", - sprintf( - "
- %s -
%s
". - "
", - Convert::raw2htmlname($this->Name), - Convert::raw2att($classes), - $label, - $this->dbObject('Content')->forTemplate() - ) + $content = LiteralField::create( + "LiteralFieldContent-{$this->ID}]", + $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; } + protected function updateFormField($field) + { + parent::updateFormField($field); + + if ($this->HideLabel) { + $this->ExtraClass .= ' nolabel'; + } else { + $field->setTitle($this->Title); + } + } + public function showInReports() { return ! $this->HideFromReports; diff --git a/templates/forms/UserFormsLiteralField_holder.ss b/templates/forms/UserFormsLiteralField_holder.ss new file mode 100644 index 0000000..910e0fe --- /dev/null +++ b/templates/forms/UserFormsLiteralField_holder.ss @@ -0,0 +1,10 @@ +
+ <% if $Title %><% end_if %> +
+ <% loop $FieldList %> + $Field + <% end_loop %> +
+ <% if $RightTitle %>$RightTitle<% end_if %> + <% if $Message %>$Message<% end_if %> +
diff --git a/tests/EditableLiteralFieldTest.php b/tests/EditableLiteralFieldTest.php index 9dd31ae..697d09e 100644 --- a/tests/EditableLiteralFieldTest.php +++ b/tests/EditableLiteralFieldTest.php @@ -38,9 +38,39 @@ class EditableLiteralFieldTest extends SapphireTest '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; - $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' + ); } }