/
AbstractLinkField.php
233 lines (207 loc) · 7.34 KB
/
AbstractLinkField.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php declare(strict_types=1);
namespace SilverStripe\LinkField\Form;
use DNADesign\Elemental\Controllers\ElementalAreaController;
use DNADesign\Elemental\Models\BaseElement;
use InvalidArgumentException;
use LogicException;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormField;
use SilverStripe\LinkField\Models\Link;
use SilverStripe\LinkField\Services\LinkTypeService;
use SilverStripe\ORM\DataObject;
/**
* Abstract form field for managing Link records
*/
abstract class AbstractLinkField extends FormField
{
protected $schemaComponent = 'LinkField';
protected $schemaDataType = FormField::SCHEMA_DATA_TYPE_CUSTOM;
protected $inputType = 'hidden';
private array $allowedTypes = [];
private bool $excludeLinkTextField = false;
public function setExcludeLinkTextField(bool $include): static
{
$this->excludeLinkTextField = $include;
return $this;
}
public function getExcludeLinkTextField(): bool
{
return $this->excludeLinkTextField;
}
/**
* Set which types of link are allowed for this field.
* Types must be FQCN of Link subclasses.
*
* @param string[] $types
* @throws InvalidArgumentException if $types is empty or any type in the array is invalid
*/
public function setAllowedTypes(array $types): static
{
$this->validateTypes($types);
$this->allowedTypes = $types;
return $this;
}
/**
* Get the types of link which are allowed for this field.
*/
public function getAllowedTypes(): array
{
return $this->allowedTypes;
}
/**
* The method returns a stringified JSON object
* of available link types with additional parameters necessary
* for work on the client side.
* @throws InvalidArgumentException
*/
public function getTypesProp(): string
{
$typesList = [];
$typeDefinitions = $this->generateAllowedTypes();
$allTypes = LinkTypeService::create()->generateAllLinkTypes();
foreach ($allTypes as $key => $class) {
$type = Injector::inst()->get($class);
$allowed = array_key_exists($key, $typeDefinitions) && $type->canCreate();
$typesList[$key] = [
'key' => $key,
'title' => $type->getMenuTitle(),
'handlerName' => $type->LinkTypeHandlerName(),
'priority' => $class::config()->get('menu_priority'),
'icon' => $class::config()->get('icon'),
'allowed' => $allowed,
];
}
uasort($typesList, function ($a, $b) {
return $a['priority'] - $b['priority'];
});
return json_encode($typesList);
}
public function performReadonlyTransformation(): FormField
{
$clone = clone $this;
$clone->setReadonly(true);
return $clone;
}
public function performDisabledTransformation(): FormField
{
$clone = clone $this;
$clone->setDisabled(true);
return $clone;
}
public function getSchemaDataDefaults(): array
{
$data = parent::getSchemaDataDefaults();
$data['types'] = json_decode($this->getTypesProp());
$data['excludeLinkTextField'] = $this->getExcludeLinkTextField();
$ownerFields = $this->getOwnerFields();
$data['ownerID'] = $ownerFields['ID'];
$data['ownerClass'] = $ownerFields['Class'];
$data['ownerRelation'] = $ownerFields['Relation'];
return $data;
}
public function getSchemaStateDefaults(): array
{
$data = parent::getSchemaStateDefaults();
$data['canCreate'] = $this->getOwner()->canEdit();
$data['readonly'] = $this->isReadonly();
$data['disabled'] = $this->isDisabled();
return $data;
}
protected function getDefaultAttributes(): array
{
$attributes = parent::getDefaultAttributes();
$attributes['data-value'] = $this->Value();
$attributes['data-can-create'] = $this->getOwner()->canEdit();
$attributes['data-readonly'] = $this->isReadonly();
$attributes['data-disabled'] = $this->isDisabled();
$attributes['data-exclude-linktext-field'] = $this->getExcludeLinkTextField();
$ownerFields = $this->getOwnerFields();
$attributes['data-owner-id'] = $ownerFields['ID'];
$attributes['data-owner-class'] = $ownerFields['Class'];
$attributes['data-owner-relation'] = $ownerFields['Relation'];
return $attributes;
}
protected function getOwner(): DataObject
{
/** @var Form $form */
$form = $this->getForm();
$owner = $form->getRecord();
if (!$owner) {
throw new LogicException('Could not determine owner from form');
}
return $owner;
}
protected function getOwnerFields(): array
{
$owner = $this->getOwner();
$relation = $this->getName();
// Elemental content block
if (class_exists(BaseElement::class) && is_a($owner, BaseElement::class)) {
// Remove namespaces from inline editable blocks
// This will return an empty array for non-inline editable blocks (e.g. blocks in a gridfield)
$arr = ElementalAreaController::removeNamespacesFromFields([$relation => ''], $owner->ID);
if (!empty($arr)) {
$relation = array_key_first($arr);
}
}
return [
'ID' => $owner->ID,
'Class' => $owner::class,
'Relation' => $relation,
];
}
/**
* Generate allowed types with key => value pair
* Example: ['cms' => SiteTreeLink::class]
* @param string[] $types
*/
private function generateAllowedTypes(): array
{
$typeDefinitions = $this->getAllowedTypes() ?? [];
if (empty($typeDefinitions)) {
return LinkTypeService::create()->generateAllLinkTypes();
}
$result = array();
foreach ($typeDefinitions as $class) {
if (is_subclass_of($class, Link::class)) {
$type = Injector::inst()->get($class)->getShortCode();
$result[$type] = $class;
}
}
return $result;
}
/**
* Validate types that they are subclasses of Link
* @param string[] $types
* @throws InvalidArgumentException
*/
private function validateTypes(array $types): void
{
if (empty($types)) {
throw new InvalidArgumentException(
_t(
__TRAIT__ . '.INVALID_TYPECLASS_EMPTY',
'"{class}": Allowed types cannot be empty',
['class' => static::class],
),
);
}
foreach ($types as $type) {
if (!is_subclass_of($type, Link::class)) {
throw new InvalidArgumentException(
_t(
__TRAIT__ . '.INVALID_TYPECLASS',
'"{class}": {typeclass} is not a valid Link Type',
['class' => static::class, 'typeclass' => $type],
sprintf(
'"%s": %s is not a valid Link Type',
static::class,
$type,
),
),
);
}
}
}
}