1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Route Map plugin for Craft CMS |
4
|
|
|
* |
5
|
|
|
* Returns a list of public routes for elements with URLs |
6
|
|
|
* |
7
|
|
|
* @link https://nystudio107.com/ |
|
|
|
|
8
|
|
|
* @copyright Copyright (c) 2017 nystudio107 |
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace nystudio107\routemap\helpers; |
12
|
|
|
|
13
|
|
|
use craft\base\Component; |
14
|
|
|
use craft\base\ElementInterface; |
|
|
|
|
15
|
|
|
use craft\base\Field as BaseField; |
16
|
|
|
use craft\elements\Entry; |
17
|
|
|
use craft\models\FieldLayout; |
18
|
|
|
use yii\base\InvalidConfigException; |
19
|
|
|
|
20
|
|
|
/** |
|
|
|
|
21
|
|
|
* @author nystudio107 |
|
|
|
|
22
|
|
|
* @package RouteMap |
|
|
|
|
23
|
|
|
* @since 1.0.0 |
|
|
|
|
24
|
|
|
*/ |
|
|
|
|
25
|
|
|
class Field extends Component |
26
|
|
|
{ |
27
|
|
|
// Static Methods |
28
|
|
|
// ========================================================================= |
29
|
|
|
/** |
30
|
|
|
* Return all the fields in the $element of the type $fieldType class |
31
|
|
|
* |
32
|
|
|
* @param ElementInterface $element |
|
|
|
|
33
|
|
|
* @param string $fieldType |
|
|
|
|
34
|
|
|
* @return array |
|
|
|
|
35
|
|
|
*/ |
36
|
|
|
public static function fieldsOfType(ElementInterface $element, string $fieldType): array |
37
|
|
|
{ |
38
|
|
|
$foundFields = []; |
39
|
|
|
|
40
|
|
|
$layout = $element->getFieldLayout(); |
41
|
|
|
if (!$layout instanceof FieldLayout) { |
42
|
|
|
return []; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$fields = $layout->getCustomFields(); |
46
|
|
|
/** @var BaseField $field */ |
|
|
|
|
47
|
|
|
foreach ($fields as $field) { |
48
|
|
|
if ($field instanceof $fieldType) { |
49
|
|
|
$foundFields[] = $field->handle; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $foundFields; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Return all the fields in the $matrixBlock of the type $fieldType class |
58
|
|
|
* |
59
|
|
|
* @param Entry $matrixEntry |
|
|
|
|
60
|
|
|
* @param string $fieldType |
|
|
|
|
61
|
|
|
* @return ?array |
|
|
|
|
62
|
|
|
*/ |
63
|
|
|
public static function matrixFieldsOfType(Entry $matrixEntry, string $fieldType): ?array |
64
|
|
|
{ |
65
|
|
|
$foundFields = []; |
66
|
|
|
|
67
|
|
|
try { |
68
|
|
|
$matrixEntryTypeModel = $matrixEntry->getType(); |
69
|
|
|
} catch (InvalidConfigException $e) { |
70
|
|
|
$matrixEntryTypeModel = null; |
71
|
|
|
} |
72
|
|
|
if ($matrixEntryTypeModel) { |
73
|
|
|
$fields = $matrixEntryTypeModel->getCustomFields(); |
74
|
|
|
/** @var BaseField $field */ |
|
|
|
|
75
|
|
|
foreach ($fields as $field) { |
76
|
|
|
if ($field instanceof $fieldType) { |
77
|
|
|
$foundFields[$field->handle] = $field->name; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $foundFields; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|