Skip to content

Commit

Permalink
Merge pull request #134 from creative-commoners/pulls/2.0/readonly-st…
Browse files Browse the repository at this point in the history
…ate-for-title

NEW Add composite field for Title and ShowTitle fields with readonly state as composite
  • Loading branch information
NightJar committed Oct 30, 2017
2 parents 8a90dc6 + 59c92d0 commit 4c6eaa6
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 5 deletions.
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'
);
}
}

0 comments on commit 4c6eaa6

Please sign in to comment.