Passed
Pull Request — master (#16)
by Robbie
02:23
created

BlockLinkField::getLinkPage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
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
    public function Type()
21
    {
22
        return 'blocklinkfield';
23
    }
24
25
    /**
26
     * Reset the cached parsed value when setting a new value
27
     *
28
     * {@inheritDoc}
29
     */
30
    public function setValue($value, $data = null)
31
    {
32
        $this->parsedValue = null;
33
        return parent::setValue($value, $data);
34
    }
35
36
    /**
37
     * Return a parsed {@link ArrayData} object from the contents of the JSON value. Will
38
     * be cached for future use, and the cache will be reset via setValue each time.
39
     *
40
     * @return ArrayData
41
     */
42
    public function getParsedValue()
43
    {
44
        if ($this->parsedValue) {
45
            return $this->parsedValue;
46
        }
47
48
        $parsedValue = Convert::json2array($this->dataValue());
49
        return $this->parsedValue = ArrayData::create((array) $parsedValue);
50
    }
51
52
    /**
53
     * Get whether a {@link SiteTree} link has been defined on this link field
54
     *
55
     * @return bool
56
     */
57
    public function getLinkDefined()
58
    {
59
        return (bool) $this->getParsedValue()->PageID;
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
74
        return DataObject::get_by_id(SiteTree::class, $pageId);
75
    }
76
77
    /**
78
     * Get the relative URL for the linked {@link SiteTree} object, with a leading slash
79
     *
80
     * @return string
81
     */
82
    public function getLinkRelativeUrl()
83
    {
84
        $page = $this->getLinkPage();
85
86
        return $page ? '/' . ltrim($page->URLSegment, '/') : '';
87
    }
88
89
    /**
90
     * Get the link text/title
91
     *
92
     * @return string
93
     */
94
    public function getLinkText()
95
    {
96
        return $this->getParsedValue()->Text;
97
    }
98
99
    /**
100
     * Get the link "description", used for titles or alt text
101
     *
102
     * @return string
103
     */
104
    public function getLinkDescription()
105
    {
106
        return $this->getParsedValue()->Description;
107
    }
108
109
    /**
110
     * Get whether to open the link in a new window
111
     *
112
     * @return bool
113
     */
114
    public function getLinkTargetBlank()
115
    {
116
        return (bool) $this->getParsedValue()->TargetBlank;
117
    }
118
}
119