nystudio107 /
craft-instantanalytics-ga4
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Instant Analytics plugin for Craft CMS |
||
| 4 | * |
||
| 5 | * @author nystudio107 |
||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
|
|||
| 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\instantanalyticsGa4\helpers; |
||
| 13 | |||
| 14 | use Craft; |
||
| 15 | use craft\base\Element; |
||
| 16 | use craft\base\Field as BaseField; |
||
| 17 | use craft\ckeditor\Field as CKEditorField; |
||
| 18 | use craft\elements\Entry; |
||
| 19 | use craft\elements\User; |
||
| 20 | use craft\fields\Assets as AssetsField; |
||
| 21 | use craft\fields\Categories as CategoriesField; |
||
| 22 | use craft\fields\Entries as EntriesField; |
||
| 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\models\Volume; |
||
| 28 | use craft\redactor\Field as RedactorField; |
||
| 29 | use Exception; |
||
| 30 | use yii\base\InvalidConfigException; |
||
| 31 | |||
| 32 | /** |
||
|
0 ignored issues
–
show
|
|||
| 33 | * @author nystudio107 |
||
|
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
|
|||
| 34 | * @package InstantAnalytics |
||
|
0 ignored issues
–
show
|
|||
| 35 | * @since 1.0.0 |
||
|
0 ignored issues
–
show
|
|||
| 36 | */ |
||
|
0 ignored issues
–
show
|
|||
| 37 | class Field |
||
| 38 | { |
||
| 39 | // Constants |
||
| 40 | // ========================================================================= |
||
| 41 | |||
| 42 | public const TEXT_FIELD_CLASS_KEY = 'text'; |
||
| 43 | public const ASSET_FIELD_CLASS_KEY = 'asset'; |
||
| 44 | public const BLOCK_FIELD_CLASS_KEY = 'block'; |
||
| 45 | |||
| 46 | protected const FIELD_CLASSES = [ |
||
| 47 | self::TEXT_FIELD_CLASS_KEY => [ |
||
| 48 | CKEditorField::class, |
||
| 49 | PlainTextField::class, |
||
| 50 | RedactorField::class, |
||
| 51 | TagsField::class, |
||
| 52 | CategoriesField::class, |
||
| 53 | EntriesField::class, |
||
| 54 | ], |
||
| 55 | self::ASSET_FIELD_CLASS_KEY => [ |
||
| 56 | AssetsField::class, |
||
| 57 | ], |
||
| 58 | self::BLOCK_FIELD_CLASS_KEY => [ |
||
| 59 | MatrixField::class, |
||
| 60 | ], |
||
| 61 | ]; |
||
| 62 | |||
| 63 | // Static Methods |
||
| 64 | // ========================================================================= |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Return all the fields from the $layout that are of the type |
||
| 68 | * $fieldClassKey |
||
| 69 | * |
||
| 70 | * @param string $fieldClassKey |
||
|
0 ignored issues
–
show
|
|||
| 71 | * @param FieldLayout $layout |
||
|
0 ignored issues
–
show
|
|||
| 72 | * @param bool $keysOnly |
||
|
0 ignored issues
–
show
|
|||
| 73 | * |
||
| 74 | * @return array |
||
| 75 | */ |
||
| 76 | public static function fieldsOfTypeFromLayout( |
||
| 77 | string $fieldClassKey, |
||
| 78 | FieldLayout $layout, |
||
| 79 | bool $keysOnly = true, |
||
| 80 | ): array { |
||
| 81 | $foundFields = []; |
||
| 82 | if (!empty(self::FIELD_CLASSES[$fieldClassKey])) { |
||
| 83 | $fieldClasses = self::FIELD_CLASSES[$fieldClassKey]; |
||
| 84 | $fields = $layout->getCustomFields(); |
||
| 85 | /** @var BaseField $field */ |
||
|
0 ignored issues
–
show
|
|||
| 86 | foreach ($fields as $field) { |
||
| 87 | /** @var array $fieldClasses */ |
||
|
0 ignored issues
–
show
|
|||
| 88 | foreach ($fieldClasses as $fieldClass) { |
||
| 89 | if ($field instanceof $fieldClass) { |
||
| 90 | $foundFields[$field->handle] = $field->name; |
||
| 91 | } |
||
| 92 | } |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | // Return only the keys if asked |
||
| 97 | if ($keysOnly) { |
||
| 98 | $foundFields = array_keys($foundFields); |
||
| 99 | } |
||
| 100 | |||
| 101 | return $foundFields; |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Return all of the fields in the $element of the type $fieldClassKey |
||
| 106 | * |
||
| 107 | * @param Element $element |
||
|
0 ignored issues
–
show
|
|||
| 108 | * @param string $fieldClassKey |
||
|
0 ignored issues
–
show
|
|||
| 109 | * @param bool $keysOnly |
||
|
0 ignored issues
–
show
|
|||
| 110 | * |
||
| 111 | * @return array |
||
| 112 | */ |
||
| 113 | public static function fieldsOfTypeFromElement( |
||
| 114 | Element $element, |
||
| 115 | string $fieldClassKey, |
||
| 116 | bool $keysOnly = true, |
||
| 117 | ): array { |
||
| 118 | $foundFields = []; |
||
| 119 | $layout = $element->getFieldLayout(); |
||
| 120 | if ($layout !== null) { |
||
| 121 | $foundFields = self::fieldsOfTypeFromLayout($fieldClassKey, $layout, $keysOnly); |
||
| 122 | } |
||
| 123 | |||
| 124 | return $foundFields; |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Return all of the fields from Users layout of the type $fieldClassKey |
||
| 129 | * |
||
| 130 | * @param string $fieldClassKey |
||
|
0 ignored issues
–
show
|
|||
| 131 | * @param bool $keysOnly |
||
|
0 ignored issues
–
show
|
|||
| 132 | * |
||
| 133 | * @return array |
||
| 134 | */ |
||
| 135 | public static function fieldsOfTypeFromUsers(string $fieldClassKey, bool $keysOnly = true): array |
||
| 136 | { |
||
| 137 | $layout = Craft::$app->getFields()->getLayoutByType(User::class); |
||
| 138 | |||
| 139 | return self::fieldsOfTypeFromLayout($fieldClassKey, $layout, $keysOnly); |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Return all the fields from all Asset Volume layouts of the type |
||
| 144 | * $fieldClassKey |
||
| 145 | * |
||
| 146 | * @param string $fieldClassKey |
||
|
0 ignored issues
–
show
|
|||
| 147 | * @param bool $keysOnly |
||
|
0 ignored issues
–
show
|
|||
| 148 | * |
||
| 149 | * @return array |
||
| 150 | */ |
||
| 151 | public static function fieldsOfTypeFromAssetVolumes(string $fieldClassKey, bool $keysOnly = true): array |
||
| 152 | { |
||
| 153 | $foundFields = []; |
||
| 154 | $volumes = Craft::$app->getVolumes()->getAllVolumes(); |
||
| 155 | foreach ($volumes as $volume) { |
||
| 156 | /** @var Volume $volume */ |
||
|
0 ignored issues
–
show
|
|||
| 157 | try { |
||
| 158 | $layout = $volume->getFieldLayout(); |
||
| 159 | } catch (Exception $e) { |
||
| 160 | $layout = null; |
||
| 161 | } |
||
| 162 | if ($layout) { |
||
| 163 | /** @noinspection SlowArrayOperationsInLoopInspection */ |
||
|
0 ignored issues
–
show
|
|||
| 164 | $foundFields = array_merge( |
||
| 165 | $foundFields, |
||
| 166 | self::fieldsOfTypeFromLayout($fieldClassKey, $layout, $keysOnly) |
||
| 167 | ); |
||
| 168 | } |
||
| 169 | } |
||
| 170 | |||
| 171 | return $foundFields; |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Return all the fields from all Global Set layouts of the type |
||
| 176 | * $fieldClassKey |
||
| 177 | * |
||
| 178 | * @param string $fieldClassKey |
||
|
0 ignored issues
–
show
|
|||
| 179 | * @param bool $keysOnly |
||
|
0 ignored issues
–
show
|
|||
| 180 | * |
||
| 181 | * @return array |
||
| 182 | */ |
||
| 183 | public static function fieldsOfTypeFromGlobals(string $fieldClassKey, bool $keysOnly = true): array |
||
| 184 | { |
||
| 185 | $foundFields = []; |
||
| 186 | $globals = Craft::$app->getGlobals()->getAllSets(); |
||
| 187 | foreach ($globals as $global) { |
||
| 188 | $layout = $global->getFieldLayout(); |
||
| 189 | /** @phpstan-ignore-next-line */ |
||
|
0 ignored issues
–
show
|
|||
| 190 | if ($layout) { |
||
| 191 | $fields = self::fieldsOfTypeFromLayout($fieldClassKey, $layout, $keysOnly); |
||
| 192 | // Prefix the keys with the global set name |
||
| 193 | $prefix = $global->handle; |
||
| 194 | $fields = array_combine( |
||
| 195 | array_map(static function($key) use ($prefix) { |
||
|
0 ignored issues
–
show
|
|||
| 196 | return $prefix . '.' . $key; |
||
| 197 | }, 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.
Loading history...
|
|||
| 198 | $fields |
||
| 199 | ); |
||
| 200 | // Merge with any fields we've already found |
||
| 201 | /** @noinspection SlowArrayOperationsInLoopInspection */ |
||
|
0 ignored issues
–
show
|
|||
| 202 | $foundFields = array_merge( |
||
| 203 | $foundFields, |
||
| 204 | $fields |
||
| 205 | ); |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | return $foundFields; |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Return all of the fields in the $matrixEntry of the type $fieldType class |
||
| 214 | * |
||
| 215 | * @param Entry $matrixEntry |
||
|
0 ignored issues
–
show
|
|||
| 216 | * @param string $fieldType |
||
|
0 ignored issues
–
show
|
|||
| 217 | * @param bool $keysOnly |
||
|
0 ignored issues
–
show
|
|||
| 218 | * |
||
| 219 | * @return array |
||
| 220 | */ |
||
| 221 | public static function matrixFieldsOfType(Entry $matrixEntry, string $fieldType, bool $keysOnly = true): array |
||
| 222 | { |
||
| 223 | $foundFields = []; |
||
| 224 | |||
| 225 | try { |
||
| 226 | $matrixEntryTypeModel = $matrixEntry->getType(); |
||
| 227 | } catch (InvalidConfigException $e) { |
||
| 228 | $matrixEntryTypeModel = null; |
||
| 229 | } |
||
| 230 | if ($matrixEntryTypeModel) { |
||
| 231 | $fields = $matrixEntryTypeModel->getCustomFields(); |
||
| 232 | /** @var BaseField $field */ |
||
|
0 ignored issues
–
show
|
|||
| 233 | foreach ($fields as $field) { |
||
| 234 | if ($field instanceof $fieldType) { |
||
| 235 | $foundFields[$field->handle] = $field->name; |
||
| 236 | } |
||
| 237 | } |
||
| 238 | // Return only the keys if asked |
||
| 239 | if ($keysOnly) { |
||
| 240 | $foundFields = array_keys($foundFields); |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | return $foundFields; |
||
| 245 | } |
||
| 246 | } |
||
| 247 |