-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElementDecoratedContent.php
More file actions
321 lines (290 loc) · 9.26 KB
/
ElementDecoratedContent.php
File metadata and controls
321 lines (290 loc) · 9.26 KB
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php
namespace NSWDPC\Elemental\Models\DecoratedContent;
use DNADesign\Elemental\Models\ElementContent;
use gorriecoe\Link\Models\Link;
use gorriecoe\LinkField\LinkField;
use SilverStripe\Assets\Image;
use SilverStripe\AssetAdmin\Forms\UploadField;
use SilverStripe\Forms\DatetimeField;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\CompositeField;
use SilverStripe\Forms\OptionsetField;
use SilverStripe\Forms\Tab;
use SilverStripe\ORM\FieldType\DBDatetime;
use SilverStripe\TagField\TagField;
use SilverStripe\Taxonomy\TaxonomyTerm;
/**
* Provides an decorated ElementContent with extra-fun fields
* @note could be added to a module
* @author James
* @author Mark
* @property ?string $Subtitle
* @property ?string $CallToAction
* @property ?string $PublicDate
* @property bool $UseLastEditedDate
* @property ?string $ImageAlignment
* @property ?string $IconClass
* @property ?string $Video
* @property ?string $Provider
* @property int $ImageID
* @property int $LinkTargetID
* @method \SilverStripe\Assets\Image Image()
* @method \gorriecoe\Link\Models\Link LinkTarget()
* @method \SilverStripe\ORM\ManyManyList<\SilverStripe\Taxonomy\TaxonomyTerm> Tags()
*/
class ElementDecoratedContent extends ElementContent
{
/**
* @inheritdoc
*/
private static bool $inline_editable = false;
/**
* @inheritdoc
*/
private static string $singular_name = 'Decorated content';
/**
* @inheritdoc
*/
private static string $plural_name = 'Decorated content';
/**
* @inheritdoc
*/
private static string $table_name = 'ElementDecoratedContent';
/**
* @inheritdoc
*/
private static string $icon = 'font-icon-block-banner';
/**
* @inheritdoc
*/
private static string $title = 'Decorated content';
/**
* @inheritdoc
*/
private static string $class_description = 'A content element with extra fields';
/**
* @inheritdoc
*/
private static array $db = [
'Subtitle' => 'Varchar(255)',
'CallToAction' => 'Varchar(32)',
'PublicDate' => 'Datetime',
'UseLastEditedDate' => 'Boolean',
'ImageAlignment' => 'Varchar(32)',
'IconClass' => 'Varchar(64)',
'Video' => 'Varchar(255)',
'Provider' => 'Varchar'
];
/**
* @inheritdoc
*/
private static array $defaults = [
'UseLastEditedDate' => 0
];
/**
* @inheritdoc
*/
private static array $has_one = [
'Image' => Image::class,
'LinkTarget' => Link::class
];
/**
* @inheritdoc
*/
private static array $many_many = [
'Tags' => TaxonomyTerm::class
];
/**
* @inheritdoc
*/
private static array $owns = [
'Image',
'LinkTarget'
];
/**
* Defines a default list of filters for the search context
*/
private static array $searchable_fields = [
'HTML'
];
/**
* Defines available video providers
*/
private static array $video_providers = [
'youtube' => 'YouTube',
'vimeo' => 'Vimeo'
];
/**
* Get available taxonomy terms
*/
protected function getTaxonomyTerms(): \SilverStripe\ORM\DataList
{
return TaxonomyTerm::get()->sort('Name ASC');
}
/**
* @inheritdoc
*/
#[\Override]
public function getType()
{
return _t(self::class . '.BlockType', 'Decorated Content');
}
/**
* @inheritdoc
*/
#[\Override]
public function onBeforeWrite()
{
parent::onBeforeWrite();
if ($this->UseLastEditedDate == 1) {
$this->PublicDate = DBDatetime::now()->Format(DBDateTime::ISO_DATETIME);
}
}
/**
* @inheritdoc
*/
#[\Override]
public function getCmsFields()
{
$fields = parent::getCmsFields();
$fields->removeByName(['PublicDate','UseLastEditedDate','Tags','LinkTargetID', 'Provider']);
$fields->insertAfter('Main', Tab::create('Image', _t(self::class . '.IMAGE', 'Image')));
$fields->addFieldsToTab(
'Root.Image',
[
UploadField::create(
'Image',
_t(self::class . '.IMAGE', 'Image')
)->setTitle(
_t(
self::class . '.ADD_IMAGE_TO_CONTENT_BLOCK',
'Add an image related to this content'
)
)->setFolderName('blocks/content/' . $this->owner->ID)
->setAllowedMaxFileNumber(1)
->setIsMultiUpload(false),
DropdownField::create(
'ImageAlignment',
_t(self::class . '.IMAGE_ALIGNMENT', 'Image alignment'),
[
'left' => _t(self::class . '.LEFT', 'Left'),
'right' => _t(self::class . '.RIGHT', 'Right')
]
)->setEmptyString('Choose an option')
->setDescription(
_t(self::class . '.IMAGE_ALIGNMENT_DESCRIPTION', 'Use of this option is dependent on the theme')
)
]
);
// Video fields
$fields->insertAfter('Image', Tab::create('Video', _t(self::class . '.VIDEO', 'Video')));
$fields->addFieldsToTab(
'Root.Video',
[
OptionsetField::create(
'Provider',
_t(self::class . '.PROVIDER', 'Video provider'),
$this->config()->get('video_providers')
),
TextField::create(
'Video',
_t(
self::class . 'VIDEO_PROVIDER_ID',
"Provider's video identification code"
)
)->setDescription(
_t(
self::class . 'VIDEO_PROVIDER_ID_DESCRIPTION',
"This is the id number or code for the video, eg '123456' or 'abcd1234' displayed by the provider, usually found in their share widget"
)
)
]
);
$fields->insertAfter('Video', Tab::create('Meta', _t(self::class . '.META', 'Meta')));
$fields->addFieldsToTab(
'Root.Meta',
[
CompositeField::create(
DatetimeField::create(
'PublicDate',
_t(self::class . '.VISIBLE_DATE', 'Date visible in element')
),
CheckboxField::create(
'UseLastEditedDate',
_t(self::class . '.USE_LASTEDITED_DATE', 'Just use the last edited date of this record')
)
)->setTitle(_t(self::class . '.DATE_OPTIONS', 'Date options')),
Tagfield::create(
'Tags',
_t(self::class . '.TAGS', 'Tags'),
[],
$this->Tags(),
'Name' // TaxonomyTerm.Name
)->setShouldLazyLoad(true)
->setCanCreate(true)
->setSourceList($this->getTaxonomyTerms()),
TextField::create(
'IconClass',
_t(self::class . '.ICON_CLASS', 'An icon class, reference or ligature')
)->setDescription(
_t(self::class . '.ICON_CLASS_DESCRIPTION', 'Use of this option is dependent on the theme in use')
)
]
);
$fields->insertAfter(
'Title',
TextField::create(
'Subtitle',
_t(self::class . '.SUBTITLE', 'Subtitle')
)->setDescription(
_t(self::class . '.SUBTITLE_DESCRIPTION', 'An optional sub-title, such as a byline. The display of this field is dependent on the theme in use')
)
);
$fields->insertAfter(
'Subtitle',
TextField::create(
'CallToAction',
_t(self::class . '.CALL_TO_ACTION', 'Call to action text')
)->setDescription(
_t(self::class . '.CALL_TO_ACTION_DESCRIPTION', 'An optional call-to-action text to use within the element. The display of this field is dependent on the theme in use')
)
);
$fields->insertAfter(
'CallToAction',
$this->getLinkField()
);
return $fields;
}
/**
* Return the field used to handle linking
*/
protected function getLinkField(): LinkField
{
return LinkField::create(
'LinkTarget',
_t(
self::class . '.LINK',
'Link'
),
$this
)->setDescription(
_t(self::class . '.LINK_DESCRIPTION', 'Choose where this content item will link to')
);
}
/**
* Compatability method to align with other content elements
*/
public function ContentLink(): ?Link
{
return $this->LinkTarget();
}
/**
* Compatability method to align with other content elements
*/
public function ContentImage(): ?Image
{
return $this->Image();
}
}