-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #134 from creative-commoners/pulls/2.0/readonly-st…
…ate-for-title NEW Add composite field for Title and ShowTitle fields with readonly state as composite
- Loading branch information
Showing
4 changed files
with
87 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
); | ||
} | ||
} |