-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathField.php
426 lines (393 loc) · 13.6 KB
/
Field.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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
<?php
/**
* SEOmatic plugin for Craft CMS 3.x
*
* A turnkey SEO implementation for Craft CMS that is comprehensive, powerful,
* and flexible
*
* @link https://nystudio107.com
* @copyright Copyright (c) 2017 nystudio107
*/
namespace nystudio107\seomatic\helpers;
use benf\neo\elements\Block as NeoBlock;
use benf\neo\Field as NeoField;
use besteadfast\preparsefield\fields\PreparseFieldType;
use Craft;
use craft\base\Element;
use craft\base\Field as BaseField;
use craft\base\Volume;
use craft\ckeditor\Field as CKEditorField;
use craft\elements\MatrixBlock;
use craft\elements\User;
use craft\fields\Assets as AssetsField;
use craft\fields\Matrix as MatrixField;
use craft\fields\PlainText as PlainTextField;
use craft\fields\Tags as TagsField;
use craft\models\FieldLayout;
use craft\redactor\Field as RedactorField;
use nystudio107\seomatic\fields\Seomatic_Meta as Seomatic_MetaField;
use nystudio107\seomatic\fields\SeoSettings as SeoSettingsField;
use nystudio107\seomatic\Seomatic;
use nystudio107\seomatic\services\MetaBundles;
use verbb\doxter\fields\Doxter as DoxterField;
use verbb\supertable\elements\SuperTableBlockElement as SuperTableBlock;
use verbb\supertable\fields\SuperTableField;
use verbb\supertable\models\SuperTableBlockTypeModel;
use yii\base\InvalidConfigException;
/**
* @author nystudio107
* @package Seomatic
* @since 3.0.0
*/
class Field
{
// Constants
// =========================================================================
const TEXT_FIELD_CLASS_KEY = 'text';
const ASSET_FIELD_CLASS_KEY = 'asset';
const BLOCK_FIELD_CLASS_KEY = 'block';
const SEO_SETTINGS_CLASS_KEY = 'seo';
const OLD_SEOMATIC_META_CLASS_KEY = 'Seomatic_Meta';
const FIELD_CLASSES = [
self::TEXT_FIELD_CLASS_KEY => [
CKEditorField::class,
PlainTextField::class,
MatrixField::class,
RedactorField::class,
TagsField::class,
NeoField::class,
SuperTableField::class,
PreparseFieldType::class,
DoxterField::class,
],
self::ASSET_FIELD_CLASS_KEY => [
AssetsField::class,
],
self::BLOCK_FIELD_CLASS_KEY => [
MatrixField::class,
NeoField::class,
SuperTableField::class,
],
self::SEO_SETTINGS_CLASS_KEY => [
SeoSettingsField::class,
],
self::OLD_SEOMATIC_META_CLASS_KEY => [
Seomatic_MetaField::class,
],
];
// Static Properties
// =========================================================================
/**
* @var array Memoization cache
*/
public static $fieldsOfTypeFromLayoutCache = [];
/**
* @var array Memoization cache
*/
public static $matrixFieldsOfTypeCache = [];
/**
* @var array Memoization cache
*/
public static $neoFieldsOfTypeCache = [];
/**
* @var array Memoization cache
*/
public static $superTableFieldsOfTypeCache = [];
// Static Methods
// =========================================================================
/**
* Return all of the fields from the $layout that are of the type
* $fieldClassKey
*
* @param string $fieldClassKey
* @param FieldLayout $layout
* @param bool $keysOnly
*
* @return array
*/
public static function fieldsOfTypeFromLayout(
string $fieldClassKey,
FieldLayout $layout,
bool $keysOnly = true
): array {
$foundFields = [];
if (!empty(self::FIELD_CLASSES[$fieldClassKey])) {
// Cache me if you can
$memoKey = $fieldClassKey . $layout->id . ($keysOnly ? 'keys' : 'nokeys');
if (!empty(self::$fieldsOfTypeFromLayoutCache[$memoKey])) {
return self::$fieldsOfTypeFromLayoutCache[$memoKey];
}
$fieldClasses = self::FIELD_CLASSES[$fieldClassKey];
$fields = $layout->getFields();
/** @var BaseField $field */
foreach ($fields as $field) {
/** @var array $fieldClasses */
foreach ($fieldClasses as $fieldClass) {
if ($field instanceof $fieldClass) {
$foundFields[$field->handle] = $field->name;
}
}
}
// Return only the keys if asked
if ($keysOnly) {
$foundFields = array_keys($foundFields);
}
// Cache for future use
self::$fieldsOfTypeFromLayoutCache[$memoKey] = $foundFields;
}
return $foundFields;
}
/**
* Return all of the fields in the $element of the type $fieldClassKey
*
* @param Element $element
* @param string $fieldClassKey
* @param bool $keysOnly
*
* @return array
*/
public static function fieldsOfTypeFromElement(
Element $element,
string $fieldClassKey,
bool $keysOnly = true
): array {
$foundFields = [];
$layout = $element->getFieldLayout();
if ($layout !== null) {
$foundFields = self::fieldsOfTypeFromLayout($fieldClassKey, $layout, $keysOnly);
}
return $foundFields;
}
/**
* Return all of the fields from Users layout of the type $fieldClassKey
*
* @param string $fieldClassKey
* @param bool $keysOnly
*
* @return array
*/
public static function fieldsOfTypeFromUsers(string $fieldClassKey, bool $keysOnly = true): array
{
$layout = Craft::$app->getFields()->getLayoutByType(User::class);
return self::fieldsOfTypeFromLayout($fieldClassKey, $layout, $keysOnly);
}
/**
* Return all of the fields from all Asset Volume layouts of the type
* $fieldClassKey
*
* @param string $fieldClassKey
* @param bool $keysOnly
*
* @return array
*/
public static function fieldsOfTypeFromAssetVolumes(string $fieldClassKey, bool $keysOnly = true): array
{
$foundFields = [];
$volumes = Craft::$app->getVolumes()->getAllVolumes();
foreach ($volumes as $volume) {
/** @var Volume $volume */
try {
$layout = $volume->getFieldLayout();
} catch (InvalidConfigException $e) {
$layout = null;
}
if ($layout) {
/** @noinspection SlowArrayOperationsInLoopInspection */
$foundFields = array_merge(
$foundFields,
self::fieldsOfTypeFromLayout($fieldClassKey, $layout, $keysOnly)
);
}
}
return $foundFields;
}
/**
* Return all of the fields from all Global Set layouts of the type
* $fieldClassKey
*
* @param string $fieldClassKey
* @param bool $keysOnly
*
* @return array
*/
public static function fieldsOfTypeFromGlobals(string $fieldClassKey, bool $keysOnly = true): array
{
$foundFields = [];
$globals = Craft::$app->getGlobals()->getAllSets();
foreach ($globals as $global) {
$layout = $global->getFieldLayout();
if ($layout) {
$fields = self::fieldsOfTypeFromLayout($fieldClassKey, $layout, $keysOnly);
// Prefix the keys with the global set name
$prefix = $global->handle;
$fields = array_combine(
array_map(function($key) use ($prefix) {
return $prefix . '.' . $key;
}, array_keys($fields)),
$fields
);
// Merge with any fields we've already found
/** @noinspection SlowArrayOperationsInLoopInspection */
$foundFields = array_merge(
$foundFields,
$fields
);
}
}
return $foundFields;
}
/**
* Return all of the fields from the $sourceBundleType in the $sourceHandle
* of the type $fieldClassKey
*
* @param string $sourceBundleType
* @param string $sourceHandle
* @param string $fieldClassKey
* @param bool $keysOnly
* @param int|string|null $typeId
*
* @return array
*/
public static function fieldsOfTypeFromSource(
string $sourceBundleType,
string $sourceHandle,
string $fieldClassKey,
bool $keysOnly = true,
$typeId = null
): array {
$foundFields = [];
$layouts = [];
// Get the layouts
if ($sourceBundleType !== MetaBundles::GLOBAL_META_BUNDLE) {
$seoElement = Seomatic::$plugin->seoElements->getSeoElementByMetaBundleType($sourceBundleType);
if ($seoElement !== null) {
$layouts = $seoElement::fieldLayouts($sourceHandle, $typeId);
}
}
// Iterate through the layouts looking for the fields of the type $fieldType
foreach ($layouts as $layout) {
/** @noinspection SlowArrayOperationsInLoopInspection */
$foundFields = array_merge(
$foundFields,
self::fieldsOfTypeFromLayout($fieldClassKey, $layout, $keysOnly)
);
}
return $foundFields;
}
/**
* Return all of the fields in the $matrixBlock of the type $fieldType class
*
* @param MatrixBlock $matrixBlock
* @param string $fieldType
* @param bool $keysOnly
*
* @return array
*/
public static function matrixFieldsOfType(MatrixBlock $matrixBlock, string $fieldType, bool $keysOnly = true): array
{
$foundFields = [];
try {
$matrixBlockTypeModel = $matrixBlock->getType();
} catch (InvalidConfigException $e) {
$matrixBlockTypeModel = null;
}
if ($matrixBlockTypeModel) {
// Cache me if you can
$memoKey = $fieldType . $matrixBlock->id . ($keysOnly ? 'keys' : 'nokeys');
if (!empty(self::$matrixFieldsOfTypeCache[$memoKey])) {
return self::$matrixFieldsOfTypeCache[$memoKey];
}
$fields = $matrixBlockTypeModel->getFields();
/** @var BaseField $field */
foreach ($fields as $field) {
if ($field instanceof $fieldType) {
$foundFields[$field->handle] = $field->name;
}
}
// Return only the keys if asked
if ($keysOnly) {
$foundFields = array_keys($foundFields);
}
// Cache for future use
self::$matrixFieldsOfTypeCache[$memoKey] = $foundFields;
}
return $foundFields;
}
/**
* Return all of the fields in the $neoBlock of the type $fieldType class
*
* @param NeoBlock $neoBlock
* @param string $fieldType
* @param bool $keysOnly
*
* @return array
*/
public static function neoFieldsOfType(NeoBlock $neoBlock, string $fieldType, bool $keysOnly = true): array
{
$foundFields = [];
$layout = $neoBlock->getFieldLayout();
if ($layout) {
// Cache me if you can
$memoKey = $fieldType . $neoBlock->id . ($keysOnly ? 'keys' : 'nokeys');
if (!empty(self::$neoFieldsOfTypeCache[$memoKey])) {
return self::$neoFieldsOfTypeCache[$memoKey];
}
$fields = $layout->getFields();
/** @var BaseField $field */
foreach ($fields as $field) {
if ($field instanceof $fieldType) {
$foundFields[$field->handle] = $field->name;
}
}
// Return only the keys if asked
if ($keysOnly) {
$foundFields = array_keys($foundFields);
}
// Cache for future use
self::$neoFieldsOfTypeCache[$memoKey] = $foundFields;
}
return $foundFields;
}
/**
* Return all of the fields in the $superTableBlock of the type $fieldType class
*
* @param SuperTableBlock $superTableBlock
* @param string $fieldType
* @param bool $keysOnly
*
* @return array
*/
public static function superTableFieldsOfType(SuperTableBlock $superTableBlock, string $fieldType, bool $keysOnly = true): array
{
$foundFields = [];
try {
/** @var SuperTableBlockTypeModel $superTableBlockTypeModel */
$superTableBlockTypeModel = $superTableBlock->getType();
} catch (InvalidConfigException $e) {
$superTableBlockTypeModel = null;
}
if ($superTableBlockTypeModel) {
// Cache me if you can
$memoKey = $fieldType . $superTableBlock->id . ($keysOnly ? 'keys' : 'nokeys');
if (!empty(self::$superTableFieldsOfTypeCache[$memoKey])) {
return self::$superTableFieldsOfTypeCache[$memoKey];
}
// The SuperTableBlockTypeModel class lacks @mixin FieldLayoutBehavior in its annotations
/** @phpstan-ignore-next-line */
$fields = $superTableBlockTypeModel->getFields();
/** @var BaseField $field */
foreach ($fields as $field) {
if ($field instanceof $fieldType) {
$foundFields[$field->handle] = $field->name;
}
}
// Return only the keys if asked
if ($keysOnly) {
$foundFields = array_keys($foundFields);
}
// Cache for future use
self::$superTableFieldsOfTypeCache[$memoKey] = $foundFields;
}
return $foundFields;
}
}