1 | <?php |
||
2 | /** |
||
3 | * Instant Analytics plugin for Craft CMS |
||
4 | * |
||
5 | * @author nystudio107 |
||
0 ignored issues
–
show
Coding Style
introduced
by
![]() Content of the @author tag must be in the form "Display Name <[email protected]>"
![]() |
|||
6 | * @copyright Copyright (c) 2017 nystudio107 |
||
0 ignored issues
–
show
|
|||
7 | * @link http://nystudio107.com |
||
0 ignored issues
–
show
|
|||
8 | * @package InstantAnalytics |
||
0 ignored issues
–
show
|
|||
9 | * @since 1.0.0 |
||
10 | */ |
||
0 ignored issues
–
show
|
|||
11 | |||
12 | namespace nystudio107\instantanalytics\helpers; |
||
13 | |||
14 | use Craft; |
||
15 | use craft\base\Element; |
||
16 | use craft\base\Field as BaseField; |
||
17 | use craft\base\Volume; |
||
18 | use craft\elements\User; |
||
19 | use craft\ckeditor\Field as CKEditorField; |
||
0 ignored issues
–
show
The type
craft\ckeditor\Field was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
20 | use craft\elements\MatrixBlock; |
||
21 | use craft\fields\Assets as AssetsField; |
||
22 | use craft\fields\Categories as CategoriesField; |
||
23 | use craft\fields\Matrix as MatrixField; |
||
24 | use craft\fields\PlainText as PlainTextField; |
||
25 | use craft\fields\Tags as TagsField; |
||
26 | use craft\models\FieldLayout; |
||
27 | use craft\redactor\Field as RedactorField; |
||
0 ignored issues
–
show
The type
craft\redactor\Field was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
28 | |||
29 | use yii\base\InvalidConfigException; |
||
30 | |||
31 | /** |
||
0 ignored issues
–
show
|
|||
32 | * @author nystudio107 |
||
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
![]() |
|||
33 | * @package InstantAnalytics |
||
0 ignored issues
–
show
|
|||
34 | * @since 1.0.0 |
||
0 ignored issues
–
show
|
|||
35 | */ |
||
0 ignored issues
–
show
|
|||
36 | class Field |
||
37 | { |
||
38 | // Constants |
||
39 | // ========================================================================= |
||
40 | |||
41 | const TEXT_FIELD_CLASS_KEY = 'text'; |
||
42 | const ASSET_FIELD_CLASS_KEY = 'asset'; |
||
43 | const BLOCK_FIELD_CLASS_KEY = 'block'; |
||
44 | |||
45 | const FIELD_CLASSES = [ |
||
46 | self::TEXT_FIELD_CLASS_KEY => [ |
||
47 | CKEditorField::class, |
||
48 | PlainTextField::class, |
||
49 | RedactorField::class, |
||
50 | TagsField::class, |
||
51 | CategoriesField::class, |
||
52 | ], |
||
53 | self::ASSET_FIELD_CLASS_KEY => [ |
||
54 | AssetsField::class, |
||
55 | ], |
||
56 | self::BLOCK_FIELD_CLASS_KEY => [ |
||
57 | MatrixField::class, |
||
58 | ], |
||
59 | ]; |
||
60 | |||
61 | // Static Methods |
||
62 | // ========================================================================= |
||
63 | |||
64 | /** |
||
65 | * Return all of the fields from the $layout that are of the type |
||
66 | * $fieldClassKey |
||
67 | * |
||
68 | * @param string $fieldClassKey |
||
0 ignored issues
–
show
|
|||
69 | * @param FieldLayout $layout |
||
0 ignored issues
–
show
|
|||
70 | * @param bool $keysOnly |
||
0 ignored issues
–
show
|
|||
71 | * |
||
72 | * @return array |
||
73 | */ |
||
74 | public static function fieldsOfTypeFromLayout( |
||
75 | string $fieldClassKey, |
||
76 | FieldLayout $layout, |
||
77 | bool $keysOnly = true |
||
78 | ): array { |
||
79 | $foundFields = []; |
||
80 | if (!empty(self::FIELD_CLASSES[$fieldClassKey])) { |
||
81 | $fieldClasses = self::FIELD_CLASSES[$fieldClassKey]; |
||
82 | $fields = $layout->getFields(); |
||
83 | /** @var $field BaseField */ |
||
0 ignored issues
–
show
|
|||
84 | foreach ($fields as $field) { |
||
85 | /** @var array $fieldClasses */ |
||
0 ignored issues
–
show
|
|||
86 | foreach ($fieldClasses as $fieldClass) { |
||
87 | if ($field instanceof $fieldClass) { |
||
88 | $foundFields[$field->handle] = $field->name; |
||
89 | } |
||
90 | } |
||
91 | } |
||
92 | } |
||
93 | |||
94 | // Return only the keys if asked |
||
95 | if ($keysOnly) { |
||
96 | $foundFields = array_keys($foundFields); |
||
97 | } |
||
98 | |||
99 | return $foundFields; |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * Return all of the fields in the $element of the type $fieldClassKey |
||
104 | * |
||
105 | * @param Element $element |
||
0 ignored issues
–
show
|
|||
106 | * @param string $fieldClassKey |
||
0 ignored issues
–
show
|
|||
107 | * @param bool $keysOnly |
||
0 ignored issues
–
show
|
|||
108 | * |
||
109 | * @return array |
||
110 | */ |
||
111 | public static function fieldsOfTypeFromElement( |
||
112 | Element $element, |
||
113 | string $fieldClassKey, |
||
114 | bool $keysOnly = true |
||
115 | ): array { |
||
116 | $foundFields = []; |
||
117 | $layout = $element->getFieldLayout(); |
||
118 | if ($layout !== null) { |
||
119 | $foundFields = self::fieldsOfTypeFromLayout($fieldClassKey, $layout, $keysOnly); |
||
120 | } |
||
121 | |||
122 | return $foundFields; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Return all of the fields from Users layout of the type $fieldClassKey |
||
127 | * |
||
128 | * @param string $fieldClassKey |
||
0 ignored issues
–
show
|
|||
129 | * @param bool $keysOnly |
||
0 ignored issues
–
show
|
|||
130 | * |
||
131 | * @return array |
||
132 | */ |
||
133 | public static function fieldsOfTypeFromUsers(string $fieldClassKey, bool $keysOnly = true): array |
||
134 | { |
||
135 | $layout = Craft::$app->getFields()->getLayoutByType(User::class); |
||
136 | |||
137 | return self::fieldsOfTypeFromLayout($fieldClassKey, $layout, $keysOnly); |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * Return all of the fields from all Asset Volume layouts of the type |
||
142 | * $fieldClassKey |
||
143 | * |
||
144 | * @param string $fieldClassKey |
||
0 ignored issues
–
show
|
|||
145 | * @param bool $keysOnly |
||
0 ignored issues
–
show
|
|||
146 | * |
||
147 | * @return array |
||
148 | */ |
||
149 | public static function fieldsOfTypeFromAssetVolumes(string $fieldClassKey, bool $keysOnly = true): array |
||
150 | { |
||
151 | $foundFields = []; |
||
152 | $volumes = Craft::$app->getVolumes()->getAllVolumes(); |
||
153 | foreach ($volumes as $volume) { |
||
154 | /** @var Volume $volume */ |
||
0 ignored issues
–
show
|
|||
155 | try { |
||
156 | $layout = $volume->getFieldLayout(); |
||
157 | } catch (InvalidConfigException $e) { |
||
158 | $layout = null; |
||
159 | } |
||
160 | if ($layout) { |
||
161 | /** @noinspection SlowArrayOperationsInLoopInspection */ |
||
0 ignored issues
–
show
|
|||
162 | $foundFields = array_merge( |
||
163 | $foundFields, |
||
164 | self::fieldsOfTypeFromLayout($fieldClassKey, $layout, $keysOnly) |
||
165 | ); |
||
166 | } |
||
167 | } |
||
168 | |||
169 | return $foundFields; |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Return all of the fields from all Global Set layouts of the type |
||
174 | * $fieldClassKey |
||
175 | * |
||
176 | * @param string $fieldClassKey |
||
0 ignored issues
–
show
|
|||
177 | * @param bool $keysOnly |
||
0 ignored issues
–
show
|
|||
178 | * |
||
179 | * @return array |
||
180 | */ |
||
181 | public static function fieldsOfTypeFromGlobals(string $fieldClassKey, bool $keysOnly = true): array |
||
182 | { |
||
183 | $foundFields = []; |
||
184 | $globals = Craft::$app->getGlobals()->getAllSets(); |
||
185 | foreach ($globals as $global) { |
||
186 | $layout = $global->getFieldLayout(); |
||
187 | if ($layout) { |
||
188 | $fields = self::fieldsOfTypeFromLayout($fieldClassKey, $layout, $keysOnly); |
||
189 | // Prefix the keys with the global set name |
||
190 | $prefix = $global->handle; |
||
191 | $fields = array_combine( |
||
192 | array_map(function ($key) use ($prefix) { |
||
0 ignored issues
–
show
|
|||
193 | return $prefix.'.'.$key; |
||
194 | }, array_keys($fields)), |
||
0 ignored issues
–
show
For multi-line function calls, the closing parenthesis should be on a new line.
If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line: someFunctionCall(
$firstArgument,
$secondArgument,
$thirdArgument
); // Closing parenthesis on a new line.
![]() |
|||
195 | $fields |
||
196 | ); |
||
197 | // Merge with any fields we've already found |
||
198 | /** @noinspection SlowArrayOperationsInLoopInspection */ |
||
0 ignored issues
–
show
|
|||
199 | $foundFields = array_merge( |
||
200 | $foundFields, |
||
201 | $fields |
||
202 | ); |
||
203 | } |
||
204 | } |
||
205 | |||
206 | return $foundFields; |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * Return all of the fields in the $matrixBlock of the type $fieldType class |
||
211 | * |
||
212 | * @param MatrixBlock $matrixBlock |
||
0 ignored issues
–
show
|
|||
213 | * @param string $fieldType |
||
0 ignored issues
–
show
|
|||
214 | * @param bool $keysOnly |
||
0 ignored issues
–
show
|
|||
215 | * |
||
216 | * @return array |
||
217 | */ |
||
218 | public static function matrixFieldsOfType(MatrixBlock $matrixBlock, string $fieldType, bool $keysOnly = true): array |
||
219 | { |
||
220 | $foundFields = []; |
||
221 | |||
222 | try { |
||
223 | $matrixBlockTypeModel = $matrixBlock->getType(); |
||
224 | } catch (InvalidConfigException $e) { |
||
225 | $matrixBlockTypeModel = null; |
||
226 | } |
||
227 | if ($matrixBlockTypeModel) { |
||
228 | $fields = $matrixBlockTypeModel->getFields(); |
||
229 | /** @var $field BaseField */ |
||
0 ignored issues
–
show
|
|||
230 | foreach ($fields as $field) { |
||
231 | if ($field instanceof $fieldType) { |
||
232 | $foundFields[$field->handle] = $field->name; |
||
233 | } |
||
234 | } |
||
235 | } |
||
236 | |||
237 | // Return only the keys if asked |
||
238 | if ($keysOnly) { |
||
239 | $foundFields = array_keys($foundFields); |
||
240 | } |
||
241 | |||
242 | return $foundFields; |
||
243 | } |
||
244 | } |
||
245 |