BlockLinkField_Readonly   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 61
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A FieldHolder() 0 3 1
A Field() 0 49 2
1
<?php
2
3
namespace SilverStripe\ElementalBannerBlock\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
    public function Field($properties = [])
20
    {
21
        /** @var BlockLinkField $originalField */
22
        $originalField = BlockLinkField::create('TempReadonly')->setValue($this->value);
23
24
        $name = $this->getName();
25
26
        $fields = FieldList::create();
27
28
        $fields->push(
29
            TreeDropdownField::create($name . '_PageID', null, SiteTree::class)
30
                ->setTitle(_t('SilverStripe\\ElementalBannerBlock\\Form\\BlockLinkField.SelectPage', 'Select a page'))
31
                ->setValue($originalField->getParsedValue()->PageID)
32
                ->performReadonlyTransformation()
33
        );
34
35
        if ($originalField->getShowLinkText()) {
36
            $fields->push(
37
                $this->castedCopy(TextField::class)
38
                    ->setName($name . '_Text')
39
                    ->setTitle(_t('SilverStripe\\ElementalBannerBlock\\Form\\BlockLinkField.LinkText', 'Link text'))
40
                    ->setValue($originalField->getLinkText())
41
            );
42
        }
43
44
        $fields->push(
45
            $this->castedCopy(TextField::class)
46
                ->setName($name . '_Description')
47
                ->setTitle(
48
                    _t('SilverStripe\\ElementalBannerBlock\\Form\\BlockLinkField.Description', 'Link description')
49
                )
50
                ->setValue($originalField->getLinkDescription())
51
        );
52
53
        $fields->push(
54
            $this->castedCopy(CheckboxField_Readonly::class)
55
                ->setName($name . '_TargetBlank')
56
                ->setTitle(_t(
57
                    'SilverStripe\\ElementalBannerBlock\\Form\\BlockLinkField.TargetBlank',
58
                    'Open in a new window/tab'
59
                ))
60
                ->setValue($originalField->getLinkTargetBlank())
61
        );
62
63
        $fields->each(function ($field) {
64
            $field->setReadonly(true);
65
        });
66
67
        return ToggleCompositeField::create($name . '_Readonly', $this->Title(), $fields);
68
    }
69
70
    /**
71
     * Do not render a form field holder for this, just display the toggled composite field
72
     *
73
     * {@inheritDoc}
74
     */
75
    public function FieldHolder($properties = [])
76
    {
77
        return $this->Field($properties);
78
    }
79
}
80