diff --git a/src/Forms/TextCheckboxGroupField.php b/src/Forms/TextCheckboxGroupField.php new file mode 100644 index 00000000..8644986e --- /dev/null +++ b/src/Forms/TextCheckboxGroupField.php @@ -0,0 +1,35 @@ +setTitle($this->getChildren()->first()->Title()); + } + + /** + * Don't use the custom template for readonly states + * + * {@inheritDoc} + */ + public function performReadonlyTransformation() + { + $field = parent::performReadonlyTransformation(); + + $field->setTemplate(CompositeField::class); + $field->setTitle(null); + + return $field; + } +} diff --git a/src/Models/BaseElement.php b/src/Models/BaseElement.php index 6a7a0f2b..0b700730 100644 --- a/src/Models/BaseElement.php +++ b/src/Models/BaseElement.php @@ -3,6 +3,7 @@ namespace DNADesign\Elemental\Models; use Exception; +use DNADesign\Elemental\Forms\TextCheckboxGroupField; use DNADesign\Elemental\Controllers\ElementController; use SilverStripe\CMS\Controllers\CMSPageEditController; use SilverStripe\Control\Controller; @@ -13,7 +14,6 @@ use SilverStripe\Core\Manifest\ModuleResourceLoader; use SilverStripe\Forms\CheckboxField; use SilverStripe\Forms\DropdownField; -use SilverStripe\Forms\FieldGroup; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\HiddenField; use SilverStripe\Forms\NumericField; @@ -259,13 +259,11 @@ public function getCMSFields() $fields->removeByName('ShowTitle'); $fields->replaceField( 'Title', - FieldGroup::create( - TextField::create('Title', ''), + TextCheckboxGroupField::create( + TextField::create('Title', _t(__CLASS__ . '.TitleLabel', 'Title (not displayed unless specified)')), CheckboxField::create('ShowTitle', _t(__CLASS__ . '.ShowTitleLabel', 'Displayed')) ) ->setName('TitleAndDisplayed') - ->setTemplate(__CLASS__ . '\\FieldGroup') - ->setTitle(_t(__CLASS__ . '.TitleLabel', 'Title (not displayed unless specified)')) ); // Rename the "Main" tab diff --git a/templates/DNADesign/Elemental/Models/BaseElement/FieldGroup.ss b/templates/DNADesign/Elemental/Forms/TextCheckboxGroupField.ss similarity index 100% rename from templates/DNADesign/Elemental/Models/BaseElement/FieldGroup.ss rename to templates/DNADesign/Elemental/Forms/TextCheckboxGroupField.ss diff --git a/tests/Forms/TextCheckboxGroupFieldTest.php b/tests/Forms/TextCheckboxGroupFieldTest.php new file mode 100644 index 00000000..3f46dcb7 --- /dev/null +++ b/tests/Forms/TextCheckboxGroupFieldTest.php @@ -0,0 +1,49 @@ +field = new TextCheckboxGroupField( + new TextField('HelloWorld'), + new CheckboxField('Display') + ); + } + + public function testFieldIsAssignedFirstFieldsTitleInConstructor() + { + $this->assertSame('Hello World', $this->field->Title()); + } + + public function testFieldReturnsCompositeFieldTemplateOnReadonlyTransformation() + { + $this->assertSame( + TextCheckboxGroupField::class, + $this->field->getTemplates()[0], + 'Uses a custom template by default' + ); + + $readonly = $this->field->performReadonlyTransformation(); + + $this->assertSame( + CompositeField::class, + $readonly->getTemplate(), + 'Uses CompositeField template for readonly' + ); + } +}