BlockLinkField::setValue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\ElementalBlocks\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
    /**
14
     * Cache for parsed value
15
     *
16
     * @var ArrayData
17
     */
18
    protected $parsedValue;
19
20
    /**
21
     * Whether to show the "link text" field
22
     *
23
     * @var bool
24
     */
25
    protected $showLinkText = true;
26
27
    public function Type()
28
    {
29
        return 'block-link-field';
30
    }
31
32
    /**
33
     * Reset the cached parsed value when setting a new value
34
     *
35
     * {@inheritDoc}
36
     */
37
    public function setValue($value, $data = null)
38
    {
39
        $this->parsedValue = null;
40
        return parent::setValue($value, $data);
41
    }
42
43
    /**
44
     * Return a parsed {@link ArrayData} object from the contents of the JSON value. Will
45
     * be cached for future use, and the cache will be reset via setValue each time.
46
     *
47
     * @return ArrayData
48
     */
49
    public function getParsedValue()
50
    {
51
        if ($this->parsedValue) {
52
            return $this->parsedValue;
53
        }
54
55
        $parsedValue = Convert::json2array($this->dataValue());
56
        return $this->parsedValue = ArrayData::create((array) $parsedValue);
57
    }
58
59
    /**
60
     * Get whether a {@link SiteTree} link has been defined on this link field
61
     *
62
     * @return bool
63
     */
64
    public function getLinkDefined()
65
    {
66
        return (bool) $this->getParsedValue()->PageID;
67
    }
68
69
    /**
70
     * Get the linked {@link SiteTree} object, if available
71
     *
72
     * @return SiteTree|null
73
     */
74
    public function getLinkPage()
75
    {
76
        $pageId = (int) $this->getParsedValue()->PageID;
77
        if (!$pageId) {
78
            return null;
79
        }
80
81
        return DataObject::get_by_id(SiteTree::class, $pageId);
82
    }
83
84
    /**
85
     * Get the relative URL for the linked {@link SiteTree} object, with a leading slash
86
     *
87
     * @return string
88
     */
89
    public function getLinkRelativeUrl()
90
    {
91
        $page = $this->getLinkPage();
92
93
        return $page ? '/' . ltrim($page->URLSegment, '/') : '';
94
    }
95
96
    /**
97
     * Get the link text/title
98
     *
99
     * @return string
100
     */
101
    public function getLinkText()
102
    {
103
        return trim($this->getParsedValue()->Text);
104
    }
105
106
    /**
107
     * Get the link "description", used for titles or alt text
108
     *
109
     * @return string
110
     */
111
    public function getLinkDescription()
112
    {
113
        return $this->getParsedValue()->Description;
114
    }
115
116
    /**
117
     * Get whether to open the link in a new window
118
     *
119
     * @return bool
120
     */
121
    public function getLinkTargetBlank()
122
    {
123
        return (bool) $this->getParsedValue()->TargetBlank;
124
    }
125
126
    /**
127
     * Set whether to display the link text field
128
     *
129
     * @param bool $showLinkText
130
     * @return $this
131
     */
132
    public function setShowLinkText($showLinkText)
133
    {
134
        $this->showLinkText = (bool) $showLinkText;
135
136
        $this->setAttribute('data-showlinktext', (int) $this->showLinkText);
137
138
        return $this;
139
    }
140
141
    /**
142
     * Get whether to display the link text field
143
     *
144
     * @return bool
145
     */
146
    public function getShowLinkText()
147
    {
148
        return $this->showLinkText;
149
    }
150
}
151