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 link text/title |
||
78 | * |
||
79 | * @return string |
||
80 | */ |
||
81 | public function getLinkText() |
||
82 | { |
||
83 | return trim($this->getParsedValue()->Text); |
||
84 | } |
||
85 | /** |
||
86 | * Get the link "description", used for titles or alt text |
||
87 | * |
||
88 | * @return string |
||
89 | */ |
||
90 | public function getLinkDescription() |
||
91 | { |
||
92 | return $this->getParsedValue()->Description; |
||
93 | } |
||
94 | /** |
||
95 | * Get whether to open the link in a new window |
||
96 | * |
||
97 | * @return bool |
||
98 | */ |
||
99 | public function getLinkTargetBlank() |
||
100 | { |
||
101 | return (bool) $this->getParsedValue()->TargetBlank; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Get the relative URL for the linked {@link SiteTree} object, with a leading slash |
||
106 | * |
||
107 | * @return string |
||
108 | */ |
||
109 | public function getLinkRelativeUrl() |
||
110 | { |
||
111 | $page = $this->getLinkPage(); |
||
112 | return $page ? '/' . ltrim($page->URLSegment, '/') : ''; |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Set whether to display the link text field |
||
117 | * |
||
118 | * @param bool $showLinkText |
||
119 | * @return $this |
||
120 | */ |
||
121 | public function setShowLinkText($showLinkText) |
||
122 | { |
||
123 | $this->showLinkText = (bool) $showLinkText; |
||
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 | /** |
||
138 | * When not used in a React form factory context, this adds the schema and state data to SilverStripe |
||
139 | * template rendered attributes lists |
||
140 | * |
||
141 | * @return array |
||
142 | */ |
||
143 | public function getAttributes() |
||
144 | { |
||
145 | $attributes = parent::getAttributes(); |
||
146 | |||
147 | $attributes['data-schema'] = json_encode($this->getSchemaData()); |
||
148 | $attributes['data-state'] = json_encode($this->getSchemaState()); |
||
149 | |||
150 | return $attributes; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Add some extra props for the React component to work with |
||
155 | * |
||
156 | * {@inheritDoc} |
||
157 | */ |
||
158 | public function getSchemaDataDefaults() |
||
159 | { |
||
160 | $schema = parent::getSchemaDataDefaults(); |
||
161 | $schema['data'] = array_merge($schema['data'], [ |
||
162 | 'showLinkText' => $this->getShowLinkText(), |
||
163 | 'linkedPage' => $this->getLinkPage() ? $this->getLinkPage()->toMap() : [], |
||
164 | 'title' => $this->Title(), |
||
165 | ]); |
||
166 | return $schema; |
||
167 | } |
||
168 | } |
||
169 |