Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NEW Add composite field for Title and ShowTitle fields with readonly state as composite #134

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/Forms/TextCheckboxGroupField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace DNADesign\Elemental\Forms;

use SilverStripe\Forms\CompositeField;

class TextCheckboxGroupField extends CompositeField
{
/**
* Set the composite's title to that of the first child
*
* {@inheritDoc}
*/
public function __construct(...$children)
{
parent::__construct($children);

$this->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;
}
}
8 changes: 3 additions & 5 deletions src/Models/BaseElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down
49 changes: 49 additions & 0 deletions tests/Forms/TextCheckboxGroupFieldTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace DNADesign\Elemental\Tests\Forms;

use DNADesign\Elemental\Forms\TextCheckboxGroupField;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\CompositeField;
use SilverStripe\Forms\TextField;

class TextCheckboxGroupFieldTest extends SapphireTest
{
/**
* @var TextCheckboxGroupField
*/
protected $field;

protected function setUp()
{
parent::setUp();

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