Passed
Push — develop-v4 ( 2540cb...33ad2c )
by Andrew
05:27
created

OptimizedImages::init()   A

Complexity

Conditions 6
Paths 16

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 21
rs 9.2222
c 0
b 0
f 0
cc 6
nc 16
nop 0
1
<?php
2
/**
3
 * Image Optimize plugin for Craft CMS
4
 *
5
 * Automatically optimize images after they've been transformed
6
 *
7
 * @link      https://nystudio107.com
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
8
 * @copyright Copyright (c) 2017 nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
9
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
10
11
namespace nystudio107\imageoptimize\fields;
12
13
use Craft;
14
use craft\base\ElementInterface;
0 ignored issues
show
Bug introduced by
The type craft\base\ElementInterface 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. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use craft\base\Field;
16
use craft\elements\Asset;
17
use craft\fields\Matrix;
18
use craft\helpers\Html;
19
use craft\helpers\Json;
20
use craft\models\FieldLayout;
21
use craft\models\Volume;
22
use craft\validators\ArrayValidator;
23
use GraphQL\Type\Definition\Type;
24
use nystudio107\imageoptimize\assetbundles\imageoptimize\ImageOptimizeAsset;
25
use nystudio107\imageoptimize\fields\OptimizedImages as OptimizedImagesField;
26
use nystudio107\imageoptimize\gql\types\generators\OptimizedImagesGenerator;
27
use nystudio107\imageoptimize\ImageOptimize;
28
use nystudio107\imageoptimize\models\OptimizedImage;
29
use ReflectionClass;
30
use ReflectionException;
31
use verbb\supertable\fields\SuperTableField;
0 ignored issues
show
Bug introduced by
The type verbb\supertable\fields\SuperTableField 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. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
32
use yii\base\InvalidConfigException;
33
use yii\db\Exception;
34
use yii\db\Schema;
35
use function is_array;
36
use function is_string;
37
38
/** @noinspection MissingPropertyAnnotationsInspection */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
39
40
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
41
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Coding Style introduced by
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
42
 * @package   ImageOptimize
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
43
 * @since     1.2.0
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
Coding Style introduced by
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
44
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
45
class OptimizedImages extends Field
46
{
47
    // Constants
48
    // =========================================================================
49
50
    public const DEFAULT_ASPECT_RATIOS = [
51
        ['x' => 16, 'y' => 9],
52
    ];
53
    public const DEFAULT_IMAGE_VARIANTS = [
54
        [
55
            'width' => 1200,
56
            'useAspectRatio' => true,
57
            'aspectRatioX' => 16.0,
58
            'aspectRatioY' => 9.0,
59
            'retinaSizes' => ['1'],
60
            'quality' => 82,
61
            'format' => 'jpg',
62
        ],
63
    ];
64
65
    public const MAX_VOLUME_SUBFOLDERS = 30;
66
67
    // Public Properties
68
    // =========================================================================
69
70
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
71
     * @var array
72
     */
73
    public array $fieldVolumeSettings = [];
74
75
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
76
     * @var array
77
     */
78
    public array $ignoreFilesOfType = [];
79
80
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
81
     * @var bool
82
     */
83
    public bool $displayOptimizedImageVariants = true;
84
85
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
86
     * @var bool
87
     */
88
    public bool $displayDominantColorPalette = true;
89
90
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
91
     * @var bool
92
     */
93
    public bool $displayLazyLoadPlaceholderImages = true;
94
95
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
96
     * @var array
97
     */
98
    public array $variants = [];
99
100
    // Private Properties
101
    // =========================================================================
102
103
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
104
     * @var array
105
     */
106
    private array $aspectRatios = [];
0 ignored issues
show
Coding Style introduced by
Private member variable "aspectRatios" must be prefixed with an underscore
Loading history...
107
108
    // Static Methods
109
    // =========================================================================
110
111
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $config should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
112
     * @inheritdoc
113
     */
114
    public function __construct(array $config = [])
115
    {
116
        // Unset any deprecated properties
117
        if (!empty($config)) {
118
            unset($config['transformMethod'], $config['imgixDomain']);
119
        }
120
        parent::__construct($config);
121
    }
122
123
    // Public Methods
124
    // =========================================================================
125
126
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
127
     * @inheritdoc
128
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
129
    public static function displayName(): string
130
    {
131
        return 'OptimizedImages';
132
    }
133
134
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
135
     * @inheritdoc
136
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
137
    public function init(): void
138
    {
139
        parent::init();
140
141
        // Handle cases where the plugin has been uninstalled
142
        if (ImageOptimize::$plugin !== null) {
143
            $settings = ImageOptimize::$plugin->getSettings();
144
            if ($settings) {
0 ignored issues
show
introduced by
$settings is of type craft\base\Model, thus it always evaluated to true.
Loading history...
145
                if (empty($this->variants)) {
146
                    $this->variants = $settings->defaultVariants;
147
                }
148
                $this->aspectRatios = $settings->defaultAspectRatios;
149
            }
150
        }
151
        // If the user has deleted all default aspect ratios, provide a fallback
152
        if (empty($this->aspectRatios)) {
153
            $this->aspectRatios = self::DEFAULT_ASPECT_RATIOS;
154
        }
155
        // If the user has deleted all default variants, provide a fallback
156
        if (empty($this->variants)) {
157
            $this->variants = self::DEFAULT_IMAGE_VARIANTS;
158
        }
159
    }
160
161
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
162
     * @inheritdoc
163
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
164
    public function rules(): array
165
    {
166
        $rules = parent::rules();
167
        return array_merge($rules, [
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
168
            [
169
                [
170
                    'displayOptimizedImageVariants',
171
                    'displayDominantColorPalette',
172
                    'displayLazyLoadPlaceholderImages',
173
                ],
174
                'boolean',
175
            ],
176
            [
177
                [
178
                    'ignoreFilesOfType',
179
                    'variants',
180
                ],
181
                ArrayValidator::class
182
            ],
183
        ]);
0 ignored issues
show
Coding Style introduced by
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...
184
    }
185
186
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
187
     * @inheritdoc
188
     * @since 1.6.2
0 ignored issues
show
Coding Style introduced by
Tag value for @since tag indented incorrectly; expected 6 spaces but found 1
Loading history...
189
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
190
    public function getContentGqlType(): Type|array
191
    {
192
        $typeArray = OptimizedImagesGenerator::generateTypes($this);
193
194
        return [
195
            'name' => $this->handle,
196
            'description' => 'Optimized Images field',
197
            'type' => array_shift($typeArray),
198
        ];
199
    }
200
201
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
202
     * @inheritdoc
203
     * @since 1.7.0
0 ignored issues
show
Coding Style introduced by
Tag value for @since tag indented incorrectly; expected 6 spaces but found 1
Loading history...
204
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
205
    public function useFieldset(): bool
206
    {
207
        return true;
208
    }
209
210
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $isNew should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $element should have a doc-comment as per coding-style.
Loading history...
211
     * @inheritdoc
212
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
213
    public function afterElementSave(ElementInterface $element, bool $isNew): void
214
    {
215
        parent::afterElementSave($element, $isNew);
216
        // Update our OptimizedImages Field data now that the Asset has been saved
217
        // If this element is propagating, we don't need to redo the image saving for each site
218
        if ($element instanceof Asset && $element->id !== null && !$element->propagating) {
219
            // If the scenario is Asset::SCENARIO_FILEOPS or Asset::SCENARIO_MOVE (if using Craft > v3.7.1) treat it as a new asset
220
            $scenario = $element->getScenario();
221
            $request = Craft::$app->getRequest();
222
            if ($isNew || $scenario === Asset::SCENARIO_FILEOPS || $scenario === Asset::SCENARIO_MOVE) {
223
                /**
224
                 * If this is a newly uploaded/created Asset, we can save the variants
225
                 * via a queue job to prevent it from blocking
226
                 */
227
                ImageOptimize::$plugin->optimizedImages->resaveAsset($element->id);
228
            } else if (!$request->isConsoleRequest && $request->getPathInfo() === 'assets/save-image') {
229
                /**
230
                 * If it's not a newly uploaded/created Asset, check to see if the image
231
                 * itself is being updated (via the ImageEditor). If so, update the
232
                 * variants immediately so the AssetSelectorHud displays the new images
233
                 */
234
                try {
235
                    ImageOptimize::$plugin->optimizedImages->updateOptimizedImageFieldData($this, $element);
236
                } catch (Exception $e) {
237
                    Craft::error($e->getMessage(), __METHOD__);
238
                }
239
            } else {
240
                ImageOptimize::$plugin->optimizedImages->resaveAsset($element->id);
241
            }
242
        }
243
    }
244
245
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $value should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $asset should have a doc-comment as per coding-style.
Loading history...
246
     * @inheritdoc
247
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
248
    public function normalizeValue($value, ElementInterface $asset = null): mixed
249
    {
250
        // If we're passed in a string, assume it's JSON-encoded, and decode it
251
        if (is_string($value) && !empty($value)) {
252
            $value = Json::decodeIfJson($value);
253
        }
254
        // If we're passed in an array, make a model from it
255
        if (is_array($value)) {
256
            // Create a new OptimizedImage model and populate it
257
            $model = new OptimizedImage($value);
258
        } elseif ($value instanceof OptimizedImage) {
259
            $model = $value;
260
        } else {
261
            // Just create a new empty model
262
            $model = new OptimizedImage([]);
263
        }
264
265
        return $model;
266
    }
267
268
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
269
     * @inheritdoc
270
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
271
    public function getContentColumnType(): string
272
    {
273
        return Schema::TYPE_TEXT;
274
    }
275
276
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
277
     * @inheritdoc
278
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
279
    public function getSettingsHtml(): null|string
280
    {
281
        $namespace = Craft::$app->getView()->getNamespace();
282
        if (str_contains($namespace, Matrix::class) || str_contains($namespace, SuperTableField::class)) {
283
            // Render an error template, since the field only works when attached to an Asset
284
            try {
285
                return Craft::$app->getView()->renderTemplate(
286
                    'image-optimize/_components/fields/OptimizedImages_error',
287
                    [
288
                    ]
289
                );
290
            } catch (Exception $e) {
291
                Craft::error($e->getMessage(), __METHOD__);
292
            }
293
        }
294
        // Register our asset bundle
295
        try {
296
            Craft::$app->getView()->registerAssetBundle(ImageOptimizeAsset::class);
297
        } catch (InvalidConfigException $e) {
298
            Craft::error($e->getMessage(), __METHOD__);
299
        }
300
301
        try {
302
            $reflect = new ReflectionClass($this);
303
            $thisId = $reflect->getShortName();
304
        } catch (ReflectionException $e) {
305
            Craft::error($e->getMessage(), __METHOD__);
306
            $thisId = 0;
307
        }
308
        // Get our id and namespace
309
        $id = Html::id($thisId);
310
        $namespacedId = Craft::$app->getView()->namespaceInputId($id);
311
        $namespacePrefix = Craft::$app->getView()->namespaceInputName($thisId);
312
        $sizesWrapperId = Craft::$app->getView()->namespaceInputId('sizes-wrapper');
313
        $view = Craft::$app->getView();
314
        $view->registerJs(
315
            'document.addEventListener("vite-script-loaded", function (e) {' .
316
            'if (e.detail.path === "../src/web/assets/src/js/OptimizedImagesField.js") {' .
317
            'new Craft.OptimizedImagesInput(' .
318
            '"' . $namespacedId . '", ' .
319
            '"' . $namespacePrefix . '",' .
320
            '"' . $sizesWrapperId . '"' .
321
            ');' .
322
            '}' .
323
            '});'
324
        );
325
326
        // Prep our aspect ratios
327
        $aspectRatios = [];
328
        $index = 1;
329
        foreach ($this->aspectRatios as $aspectRatio) {
330
            if ($index % 6 === 0) {
331
                $aspectRatio['break'] = true;
332
            }
333
            $aspectRatios[] = $aspectRatio;
334
            $index++;
335
        }
336
        $aspectRatio = ['x' => 2, 'y' => 2, 'custom' => true];
337
        $aspectRatios[] = $aspectRatio;
338
        // Get only the user-editable settings
339
        $settings = ImageOptimize::$plugin->getSettings();
0 ignored issues
show
Bug introduced by
The method getSettings() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

339
        /** @scrutinizer ignore-call */ 
340
        $settings = ImageOptimize::$plugin->getSettings();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
340
341
        // Render the settings template
342
        try {
343
            return Craft::$app->getView()->renderTemplate(
344
                'image-optimize/_components/fields/OptimizedImages_settings',
345
                [
346
                    'field' => $this,
347
                    'settings' => $settings,
348
                    'aspectRatios' => $aspectRatios,
349
                    'id' => $id,
350
                    'name' => $this->handle,
351
                    'namespace' => $namespacedId,
352
                    'fieldVolumes' => $this->getFieldVolumeInfo($this->handle),
353
                ]
354
            );
355
        } catch (Exception $e) {
356
            Craft::error($e->getMessage(), __METHOD__);
357
        }
358
359
        return '';
360
    }
361
362
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $value should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $element should have a doc-comment as per coding-style.
Loading history...
363
     * @inheritdoc
364
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
365
    public function getInputHtml($value, ElementInterface $element = null): string
366
    {
367
        if ($element instanceof Asset && $this->handle !== null) {
368
            /** @var Asset $element */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
369
            // Register our asset bundle
370
            try {
371
                Craft::$app->getView()->registerAssetBundle(ImageOptimizeAsset::class);
372
            } catch (InvalidConfigException $e) {
373
                Craft::error($e->getMessage(), __METHOD__);
374
            }
375
376
            // Get our id and namespace
377
            $id = Html::id($this->handle);
378
            $nameSpaceId = Craft::$app->getView()->namespaceInputId($id);
379
380
            // Variables to pass down to our field JavaScript to let it namespace properly
381
            $jsonVars = [
382
                'id' => $id,
383
                'name' => $this->handle,
384
                'namespace' => $nameSpaceId,
385
                'prefix' => Craft::$app->getView()->namespaceInputId(''),
386
            ];
387
            $jsonVars = Json::encode($jsonVars);
388
            $view = Craft::$app->getView();
389
            $view->registerJs(
390
                'document.addEventListener("vite-script-loaded", function (e) {' .
391
                'if (e.detail.path === "../src/web/assets/src/js/OptimizedImagesField.js") {' .
392
                "$('#{$nameSpaceId}-field').ImageOptimizeOptimizedImages(" .
393
                $jsonVars .
394
                ");" .
395
                '}' .
396
                '});'
397
            );
398
399
            $settings = ImageOptimize::$plugin->getSettings();
400
            $createVariants = ImageOptimize::$plugin->optimizedImages->shouldCreateVariants($this, $element);
401
402
            // Render the input template
403
            try {
404
                return Craft::$app->getView()->renderTemplate(
405
                    'image-optimize/_components/fields/OptimizedImages_input',
406
                    [
407
                        'name' => $this->handle,
408
                        'value' => $value,
409
                        'variants' => $this->variants,
410
                        'field' => $this,
411
                        'settings' => $settings,
412
                        'elementId' => $element->id,
413
                        'format' => $element->getExtension(),
414
                        'id' => $id,
415
                        'nameSpaceId' => $nameSpaceId,
416
                        'createVariants' => $createVariants,
417
                    ]
418
                );
419
            } catch (Exception $e) {
420
                Craft::error($e->getMessage(), __METHOD__);
421
            }
422
        }
423
424
        // Render an error template, since the field only works when attached to an Asset
425
        try {
426
            return Craft::$app->getView()->renderTemplate(
427
                'image-optimize/_components/fields/OptimizedImages_error',
428
                [
429
                ]
430
            );
431
        } catch (Exception $e) {
432
            Craft::error($e->getMessage(), __METHOD__);
433
        }
434
435
        return '';
436
    }
437
438
    // Protected Methods
439
    // =========================================================================
440
441
    /**
442
     * Returns an array of asset volumes and their sub-folders
443
     *
444
     * @param string|null $fieldHandle
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
445
     *
446
     * @return array
447
     * @throws InvalidConfigException
448
     */
449
    protected function getFieldVolumeInfo(?string $fieldHandle): array
450
    {
451
        $result = [];
452
        if ($fieldHandle !== null) {
453
            $volumes = Craft::$app->getVolumes()->getAllVolumes();
454
            $assets = Craft::$app->getAssets();
455
            foreach ($volumes as $volume) {
456
                if (($volume instanceof Volume) && $this->volumeHasField($volume, $fieldHandle)) {
457
                    $tree = $assets->getFolderTreeByVolumeIds([$volume->id]);
458
                    $result[] = [
459
                        'name' => $volume->name,
460
                        'handle' => $volume->handle,
461
                        'subfolders' => $this->assembleSourceList($tree),
462
                    ];
463
                }
464
            }
465
        }
466
        // If there are too many sub-folders in an Asset volume, don't display them, return an empty array
467
        if (count($result) > self::MAX_VOLUME_SUBFOLDERS) {
468
            $result = [];
469
        }
470
471
        return $result;
472
    }
473
474
    /**
475
     * See if the passed $volume has an OptimizedImagesField with the handle $fieldHandle
476
     *
477
     * @param Volume $volume
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
478
     * @param string $fieldHandle
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
479
     *
480
     * @return bool
481
     */
482
    protected function volumeHasField(Volume $volume, string $fieldHandle): bool
483
    {
484
        $result = false;
485
        /** @var FieldLayout $fieldLayout */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
486
        $fieldLayout = $volume->getFieldLayout();
487
        // Loop through the fields in the layout to see if there is an OptimizedImages field
488
        if ($fieldLayout) {
0 ignored issues
show
introduced by
$fieldLayout is of type craft\models\FieldLayout, thus it always evaluated to true.
Loading history...
489
            $fields = $fieldLayout->getCustomFields();
490
            foreach ($fields as $field) {
491
                if ($field instanceof OptimizedImagesField && $field->handle === $fieldHandle) {
492
                    $result = true;
493
                }
494
            }
495
        }
496
497
        return $result;
498
    }
499
500
    /**
501
     * Transforms an asset folder tree into a source list.
502
     *
503
     * @param array $folders
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
504
     * @param bool $includeNestedFolders
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
505
     *
506
     * @return array
507
     */
508
    protected function assembleSourceList(array $folders, bool $includeNestedFolders = true): array
509
    {
510
        $sources = [];
511
512
        foreach ($folders as $folder) {
513
            $children = $folder->getChildren();
514
            foreach ($children as $child) {
515
                $sources[$child->name] = $child->name;
516
            }
517
        }
518
519
        return $sources;
520
    }
521
}
522