-
Notifications
You must be signed in to change notification settings - Fork 332
/
SiteTreeURLSegmentField.php
186 lines (160 loc) · 4.44 KB
/
SiteTreeURLSegmentField.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
namespace SilverStripe\CMS\Forms;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\Controller;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Convert;
use SilverStripe\Forms\TextField;
use SilverStripe\View\Requirements;
/**
* Used to edit the SiteTree->URLSegment property, and suggest input based on the serverside rules
* defined through {@link SiteTree->generateURLSegment()} and {@link URLSegmentFilter}.
*
* Note: The actual conversion for saving the value takes place in the model layer.
*/
class SiteTreeURLSegmentField extends TextField
{
/**
* @var string
*/
protected $helpText;
/**
* @var string
*/
protected $urlPrefix;
/**
* @var string
*/
protected $urlSuffix;
/**
* @var string
*/
protected $defaultUrl;
private static $allowed_actions = [
'suggest'
];
public function Value()
{
return rawurldecode($this->value ?? '');
}
public function getAttributes()
{
return array_merge(
parent::getAttributes(),
[
'data-prefix' => $this->getURLPrefix(),
'data-suffix' => $this->getURLSuffix(),
'data-default-url' => $this->getDefaultURL()
]
);
}
public function Field($properties = [])
{
return parent::Field($properties);
}
/**
* @param HTTPRequest $request
* @return string
*/
public function suggest($request)
{
if (!$request->getVar('value')) {
return $this->httpError(
405,
_t('SilverStripe\\CMS\\Forms\\SiteTreeURLSegmentField.EMPTY', 'Please enter a URL segment or click cancel')
);
}
$page = $this->getPage();
// Same logic as SiteTree->onBeforeWrite
$page->URLSegment = $page->generateURLSegment($request->getVar('value'));
$count = 2;
while (!$page->validURLSegment()) {
$page->URLSegment = preg_replace('/-[0-9]+$/', '', $page->URLSegment ?? '') . '-' . $count;
$count++;
}
Controller::curr()->getResponse()->addHeader('Content-Type', 'application/json');
return json_encode(['value' => $page->URLSegment]);
}
/**
* @return SiteTree
*/
public function getPage()
{
$idField = $this->getForm()->Fields()->dataFieldByName('ID');
return ($idField && $idField->Value())
? SiteTree::get()->byID($idField->Value())
: SiteTree::singleton();
}
/**
* @param string $string The secondary text to show
* @return $this
*/
public function setHelpText($string)
{
$this->helpText = $string;
return $this;
}
/**
* @return string the secondary text to show in the template
*/
public function getHelpText()
{
return $this->helpText;
}
/**
* @param string $url the url that prefixes the page url segment field
* @return $this
*/
public function setURLPrefix($url)
{
$this->urlPrefix = $url;
return $this;
}
/**
* @return string the url prefixes the page url segment field to show in template
*/
public function getURLPrefix()
{
return rtrim($this->urlPrefix ?? '', '/') . '/';
}
public function getURLSuffix()
{
return $this->urlSuffix;
}
/**
* @return string Indicator for UI to respond to changes accurately,
* and auto-update the field value if changes to the default occur.
* Does not set the field default value.
*/
public function getDefaultURL()
{
return $this->defaultUrl;
}
public function setDefaultURL($url)
{
$this->defaultUrl = $url;
return $this;
}
public function setURLSuffix($suffix)
{
$this->urlSuffix = $suffix;
return $this;
}
public function Type()
{
return 'text urlsegment';
}
public function getURL()
{
return Controller::join_links($this->getURLPrefix(), $this->Value(), $this->getURLSuffix());
}
public function performReadonlyTransformation()
{
$newInst = parent::performReadonlyTransformation();
$newInst->helpText = $this->helpText;
$newInst->urlPrefix = $this->urlPrefix;
$newInst->urlSuffix = $this->urlSuffix;
$newInst->defaultUrl = $this->defaultUrl;
return $newInst;
}
}