Completed
Push — master ( 0446fc...908d30 )
by
unknown
11s
created

BlockLinkField_Readonly   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
dl 0
loc 58
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A FieldHolder() 0 3 1
B Field() 0 46 2
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
    public function Field($properties = [])
20
    {
21
        /** @var BlockLinkField $originalField */
22
        $originalField = BlockLinkField::create('TempReadonly')->setValue($this->value);
0 ignored issues
show
Bug introduced by
'TempReadonly' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

22
        $originalField = BlockLinkField::create(/** @scrutinizer ignore-type */ 'TempReadonly')->setValue($this->value);
Loading history...
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\\ElementalBlocks\\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\\ElementalBlocks\\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(_t('SilverStripe\\ElementalBlocks\\Form\\BlockLinkField.Description', 'Link description'))
48
                ->setValue($originalField->getLinkDescription())
49
        );
50
51
        $fields->push(
52
            $this->castedCopy(CheckboxField_Readonly::class)
53
                ->setName($name . '_TargetBlank')
54
                ->setTitle(
55
                    _t('SilverStripe\\ElementalBlocks\\Form\\BlockLinkField.TargetBlank', 'Open in a new window/tab')
56
                )
57
                ->setValue($originalField->getLinkTargetBlank())
58
        );
59
60
        $fields->each(function ($field) {
61
            $field->setReadonly(true);
62
        });
63
64
        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
    public function FieldHolder($properties = [])
73
    {
74
        return $this->Field($properties);
75
    }
76
}
77