/
TextCheckboxGroupField.php
71 lines (58 loc) · 1.95 KB
/
TextCheckboxGroupField.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
namespace DNADesign\Elemental\Forms;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\CompositeField;
use SilverStripe\Forms\LiteralField;
use SilverStripe\Forms\TextField;
class TextCheckboxGroupField extends CompositeField
{
protected $schemaComponent = 'TextCheckboxGroupField';
/**
* Set the composite's title to that of the first child
*
* @param string|null $title
*/
public function __construct($title = null)
{
if (!$title) {
$title = _t(__CLASS__ . '.Title', 'Title');
}
$fields = [
TextField::create('Title', $title),
CheckboxField::create('ShowTitle', _t(__CLASS__ . '.ShowTitleLabel', 'Displayed'))
];
parent::__construct($fields);
$this->setTitle($title);
}
/**
* Don't use the custom template for readonly states
*
* {@inheritDoc}
*/
public function performReadonlyTransformation()
{
$field = $this;
if (!$this->readonly) {
$field = parent::performReadonlyTransformation();
$field->setTemplate(CompositeField::class);
$field->setTitle('Title');
$titleField = $field->fieldByName('Title');
if ($titleField) {
$field->replaceField('Title', LiteralField::create(
'Title',
$titleField->Value()
));
}
$displayedText = _t(__CLASS__ . '.DISPLAYED', 'Displayed');
$notDisplayedText = _t(__CLASS__ . '.NOT_DISPLAYED', 'Not displayed');
$showTitle = $field->fieldByName('ShowTitle');
if ($showTitle) {
$field->replaceField('ShowTitle', LiteralField::create(
'ShowTitle',
$showTitle->Value() === 'Yes' ? $displayedText : $notDisplayedText
)->addExtraClass('show-title'));
}
}
return $field;
}
}