1
|
|
<?php
|
2
|
|
|
3
|
|
namespace SilverStripe\ElementalBlocks\Form;
|
4
|
|
|
5
|
|
use SilverStripe\CMS\Model\SiteTree;
|
6
|
|
use SilverStripe\Forms\CheckboxField_Readonly;
|
7
|
|
use SilverStripe\Forms\FieldList;
|
8
|
|
use SilverStripe\Forms\ReadonlyField;
|
9
|
|
use SilverStripe\Forms\TextField;
|
10
|
|
use SilverStripe\Forms\ToggleCompositeField;
|
11
|
|
use SilverStripe\Forms\TreeDropdownField;
|
12
|
|
|
13
|
|
/**
|
14
|
|
* Readonly version of a {@link BlockLinkField} field, which displays the data fields as readonly text
|
15
|
|
* inputs and a checkbox for "target blank".
|
16
|
|
*/
|
17
|
|
class BlockLinkField_Readonly extends ReadonlyField
|
18
|
|
{
|
19
|
2
|
public function Field($properties = [])
|
20
|
|
{
|
21
|
|
/** @var BlockLinkField $originalField */
|
22
|
2
|
$originalField = BlockLinkField::create('TempReadonly')->setValue($this->value);
|
23
|
|
|
24
|
2
|
$name = $this->getName();
|
25
|
|
|
26
|
2
|
$fields = FieldList::create();
|
27
|
|
|
28
|
2
|
$fields->push(
|
29
|
2
|
TreeDropdownField::create($name . '_PageID', null, SiteTree::class)
|
30
|
2
|
->setTitle(_t('SilverStripe\\ElementalBlocks\\Form\\BlockLinkField.SelectPage', 'Select a page'))
|
31
|
2
|
->setValue($originalField->getParsedValue()->PageID)
|
32
|
2
|
->performReadonlyTransformation()
|
33
|
|
);
|
34
|
|
|
35
|
2
|
if ($originalField->getShowLinkText()) {
|
36
|
2
|
$fields->push(
|
37
|
2
|
$this->castedCopy(TextField::class)
|
38
|
2
|
->setName($name . '_Text')
|
39
|
2
|
->setTitle(_t('SilverStripe\\ElementalBlocks\\Form\\BlockLinkField.LinkText', 'Link text'))
|
40
|
2
|
->setValue($originalField->getLinkText())
|
41
|
|
);
|
42
|
|
}
|
43
|
|
|
44
|
2
|
$fields->push(
|
45
|
2
|
$this->castedCopy(TextField::class)
|
46
|
2
|
->setName($name . '_Description')
|
47
|
2
|
->setTitle(_t('SilverStripe\\ElementalBlocks\\Form\\BlockLinkField.Description', 'Link description'))
|
48
|
2
|
->setValue($originalField->getLinkDescription())
|
49
|
|
);
|
50
|
|
|
51
|
2
|
$fields->push(
|
52
|
2
|
$this->castedCopy(CheckboxField_Readonly::class)
|
53
|
2
|
->setName($name . '_TargetBlank')
|
54
|
2
|
->setTitle(
|
55
|
2
|
_t('SilverStripe\\ElementalBlocks\\Form\\BlockLinkField.TargetBlank', 'Open in a new window/tab')
|
56
|
|
)
|
57
|
2
|
->setValue($originalField->getLinkTargetBlank())
|
58
|
|
);
|
59
|
|
|
60
|
2
|
$fields->each(function ($field) {
|
61
|
2
|
$field->setReadonly(true);
|
62
|
2
|
});
|
63
|
|
|
64
|
2
|
return ToggleCompositeField::create($name . '_Readonly', $this->Title(), $fields);
|
65
|
|
}
|
66
|
|
|
67
|
|
/**
|
68
|
|
* Do not render a form field holder for this, just display the toggled composite field
|
69
|
|
*
|
70
|
|
* {@inheritDoc}
|
71
|
|
*/
|
72
|
0
|
public function FieldHolder($properties = [])
|
73
|
|
{
|
74
|
0
|
return $this->Field($properties);
|
75
|
|
}
|
76
|
|
}
|