Completed
Push — master ( f895d7...362b24 )
by
unknown
22s
created

BlockLinkField::getAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\ElementalBannerBlock\Form;
4
5
use SilverStripe\CMS\Model\SiteTree;
6
use SilverStripe\Core\Convert;
7
use SilverStripe\Forms\FormField;
8
use SilverStripe\ORM\DataObject;
9
use SilverStripe\View\ArrayData;
10
11
class BlockLinkField extends FormField
12
{
13
    protected $schemaDataType = FormField::SCHEMA_DATA_TYPE_CUSTOM;
14
15
    protected $schemaComponent = 'BlockLinkField';
16
17
    /**
18
     * Cache for parsed value
19
     *
20
     * @var ArrayData
21
     */
22
    protected $parsedValue;
23
24
    /**
25
     * Whether to show the "link text" field
26
     *
27
     * @var bool
28
     */
29
    protected $showLinkText = true;
30
31
    public function Type()
32
    {
33
        return 'block-link-field';
34
    }
35
36
    /**
37
     * Reset the cached parsed value when setting a new value
38
     *
39
     * {@inheritDoc}
40
     */
41
    public function setValue($value, $data = null)
42
    {
43
        $this->parsedValue = null;
44
        return parent::setValue($value, $data);
45
    }
46
47
    /**
48
     * Return a parsed {@link ArrayData} object from the contents of the JSON value. Will
49
     * be cached for future use, and the cache will be reset via setValue each time.
50
     *
51
     * @return ArrayData
52
     */
53
    public function getParsedValue()
54
    {
55
        if ($this->parsedValue) {
56
            return $this->parsedValue;
57
        }
58
        $parsedValue = json_decode($this->dataValue(), true);
59
        return $this->parsedValue = ArrayData::create((array) $parsedValue);
60
    }
61
62
    /**
63
     * Get the linked {@link SiteTree} object, if available
64
     *
65
     * @return SiteTree|null
66
     */
67
    public function getLinkPage()
68
    {
69
        $pageId = (int) $this->getParsedValue()->PageID;
70
        if (!$pageId) {
71
            return null;
72
        }
73
        return DataObject::get_by_id(SiteTree::class, $pageId);
74
    }
75
76
    /**
77
     * Get the relative URL for the linked {@link SiteTree} object, with a leading slash
78
     *
79
     * @return string
80
     */
81
    public function getLinkRelativeUrl()
82
    {
83
        $page = $this->getLinkPage();
84
        return $page ? '/' . ltrim($page->URLSegment, '/') : '';
0 ignored issues
show
introduced by
$page is of type SilverStripe\CMS\Model\SiteTree, thus it always evaluated to true.
Loading history...
85
    }
86
87
    /**
88
     * Set whether to display the link text field
89
     *
90
     * @param bool $showLinkText
91
     * @return $this
92
     */
93
    public function setShowLinkText($showLinkText)
94
    {
95
        $this->showLinkText = (bool) $showLinkText;
96
        return $this;
97
    }
98
99
    /**
100
     * Get whether to display the link text field
101
     *
102
     * @return bool
103
     */
104
    public function getShowLinkText()
105
    {
106
        return $this->showLinkText;
107
    }
108
109
    /**
110
     * When not used in a React form factory context, this adds the schema and state data to SilverStripe
111
     * template rendered attributes lists
112
     *
113
     * @return array
114
     */
115
    public function getAttributes()
116
    {
117
        $attributes = parent::getAttributes();
118
119
        $attributes['data-schema'] = json_encode($this->getSchemaData());
120
        $attributes['data-state'] = json_encode($this->getSchemaState());
121
122
        return $attributes;
123
    }
124
125
    /**
126
     * Add some extra props for the React component to work with
127
     *
128
     * {@inheritDoc}
129
     */
130
    public function getSchemaDataDefaults()
131
    {
132
        $schema = parent::getSchemaDataDefaults();
133
        $schema['data'] = array_merge($schema['data'], [
134
            'showLinkText' => $this->getShowLinkText(),
135
            'linkedPage' => $this->getLinkPage() ? $this->getLinkPage()->toMap() : [],
136
            'title' => $this->Title(),
137
        ]);
138
        return $schema;
139
    }
140
}
141