Completed
Pull Request — master (#13)
by Robbie
01:50
created

BlockLinkField::getShowLinkText()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 1
nc 1
nop 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 'blocklinkfield';
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 relative URL for the linked {@link SiteTree} object, with a leading slash
71
     *
72
     * @return string
73
     */
74
    public function getLinkRelativeUrl()
75
    {
76
        /** @var SiteTree $page */
77
        $page = DataObject::get_by_id(SiteTree::class, $this->getParsedValue()->PageID);
78
79
        return $page ? '/' . ltrim($page->URLSegment, '/') : '';
80
    }
81
82
    /**
83
     * Get the link text/title
84
     *
85
     * @return string
86
     */
87
    public function getLinkText()
88
    {
89
        return trim($this->getParsedValue()->Text);
90
    }
91
92
    /**
93
     * Get the link "description", used for titles or alt text
94
     *
95
     * @return string
96
     */
97
    public function getLinkDescription()
98
    {
99
        return $this->getParsedValue()->Description;
100
    }
101
102
    /**
103
     * Get whether to open the link in a new window
104
     *
105
     * @return bool
106
     */
107
    public function getLinkTargetBlank()
108
    {
109
        return (bool) $this->getParsedValue()->TargetBlank;
110
    }
111
112
    /**
113
     * Set whether to display the link text field
114
     *
115
     * @param bool $showLinkText
116
     * @return $this
117
     */
118
    public function setShowLinkText($showLinkText)
119
    {
120
        $this->showLinkText = (bool) $showLinkText;
121
122
        $this->setAttribute('data-showlinktext', (int) $this->showLinkText);
123
124
        return $this;
125
    }
126
127
    /**
128
     * Get whether to display the link text field
129
     *
130
     * @return bool
131
     */
132
    public function getShowLinkText()
133
    {
134
        return $this->showLinkText;
135
    }
136
}
137