Issues (653)

src/helpers/Field.php (67 issues)

1
<?php
2
/**
3
 * Instant Analytics plugin for Craft CMS
4
 *
5
 * @author    nystudio107
0 ignored issues
show
The tag in position 1 should be the @package tag
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
The tag in position 2 should be the @author tag
Loading history...
@copyright tag must contain a year and the name of the copyright holder
Loading history...
7
 * @link      http://nystudio107.com
0 ignored issues
show
The tag in position 3 should be the @copyright tag
Loading history...
8
 * @package   InstantAnalytics
0 ignored issues
show
The tag in position 4 should be the @link tag
Loading history...
9
 * @since     1.0.0
10
 */
0 ignored issues
show
PHP version not specified
Loading history...
Missing @category tag in file comment
Loading history...
Missing @license tag in file comment
Loading history...
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
Missing short description in doc comment
Loading history...
33
 * @author    nystudio107
0 ignored issues
show
The tag in position 1 should be the @package tag
Loading history...
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
34
 * @package   InstantAnalytics
0 ignored issues
show
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
35
 * @since     1.0.0
0 ignored issues
show
The tag in position 3 should be the @author tag
Loading history...
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
36
 */
0 ignored issues
show
Missing @category tag in class comment
Loading history...
Missing @license tag in class comment
Loading history...
Missing @link tag in class comment
Loading history...
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
Missing parameter comment
Loading history...
Expected 6 spaces after parameter type; 1 found
Loading history...
71
     * @param FieldLayout $layout
0 ignored issues
show
Missing parameter comment
Loading history...
72
     * @param bool $keysOnly
0 ignored issues
show
Missing parameter comment
Loading history...
Expected 8 spaces after parameter type; 1 found
Loading history...
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
The open comment tag must be the only content on the line
Loading history...
Missing short description in doc comment
Loading history...
The close comment tag must be the only content on the line
Loading history...
86
            foreach ($fields as $field) {
87
                /** @var array $fieldClasses */
0 ignored issues
show
The open comment tag must be the only content on the line
Loading history...
Missing short description in doc comment
Loading history...
The close comment tag must be the only content on the line
Loading history...
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
Missing parameter comment
Loading history...
108
     * @param string $fieldClassKey
0 ignored issues
show
Missing parameter comment
Loading history...
Expected 2 spaces after parameter type; 1 found
Loading history...
109
     * @param bool $keysOnly
0 ignored issues
show
Missing parameter comment
Loading history...
Expected 4 spaces after parameter type; 1 found
Loading history...
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
Missing parameter comment
Loading history...
131
     * @param bool $keysOnly
0 ignored issues
show
Missing parameter comment
Loading history...
Expected 3 spaces after parameter type; 1 found
Loading history...
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
Missing parameter comment
Loading history...
147
     * @param bool $keysOnly
0 ignored issues
show
Missing parameter comment
Loading history...
Expected 3 spaces after parameter type; 1 found
Loading history...
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
The open comment tag must be the only content on the line
Loading history...
Missing short description in doc comment
Loading history...
The close comment tag must be the only content on the line
Loading history...
157
            try {
158
                $layout = $volume->getFieldLayout();
159
            } catch (Exception $e) {
160
                $layout = null;
161
            }
162
            if ($layout) {
163
                /** @noinspection SlowArrayOperationsInLoopInspection */
0 ignored issues
show
The open comment tag must be the only content on the line
Loading history...
Missing short description in doc comment
Loading history...
The close comment tag must be the only content on the line
Loading history...
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
Missing parameter comment
Loading history...
179
     * @param bool $keysOnly
0 ignored issues
show
Missing parameter comment
Loading history...
Expected 3 spaces after parameter type; 1 found
Loading history...
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
The open comment tag must be the only content on the line
Loading history...
Missing short description in doc comment
Loading history...
The close comment tag must be the only content on the line
Loading history...
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
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
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
The open comment tag must be the only content on the line
Loading history...
Missing short description in doc comment
Loading history...
The close comment tag must be the only content on the line
Loading history...
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
Missing parameter comment
Loading history...
Expected 2 spaces after parameter type; 1 found
Loading history...
216
     * @param string $fieldType
0 ignored issues
show
Missing parameter comment
Loading history...
217
     * @param bool $keysOnly
0 ignored issues
show
Missing parameter comment
Loading history...
Expected 3 spaces after parameter type; 1 found
Loading history...
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
The open comment tag must be the only content on the line
Loading history...
Missing short description in doc comment
Loading history...
The close comment tag must be the only content on the line
Loading history...
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