1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Image Optimize plugin for Craft CMS 3.x |
4
|
|
|
* |
5
|
|
|
* Automatically optimize images after they've been transformed |
6
|
|
|
* |
7
|
|
|
* @link https://nystudio107.com |
|
|
|
|
8
|
|
|
* @copyright Copyright (c) 2017 nystudio107 |
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace nystudio107\imageoptimize\fields; |
12
|
|
|
|
13
|
|
|
use Craft; |
14
|
|
|
use craft\base\ElementInterface; |
15
|
|
|
use craft\base\Field; |
16
|
|
|
use craft\base\Volume; |
17
|
|
|
use craft\elements\Asset; |
18
|
|
|
use craft\fields\Matrix; |
19
|
|
|
use craft\helpers\Html; |
20
|
|
|
use craft\helpers\Json; |
21
|
|
|
use craft\models\FieldLayout; |
22
|
|
|
use craft\validators\ArrayValidator; |
23
|
|
|
use nystudio107\imageoptimize\assetbundles\imageoptimize\ImageOptimizeAsset; |
24
|
|
|
use nystudio107\imageoptimize\fields\OptimizedImages as OptimizedImagesField; |
25
|
|
|
use nystudio107\imageoptimize\gql\types\generators\OptimizedImagesGenerator; |
26
|
|
|
use nystudio107\imageoptimize\ImageOptimize; |
27
|
|
|
use nystudio107\imageoptimize\models\OptimizedImage; |
28
|
|
|
use ReflectionClass; |
29
|
|
|
use ReflectionException; |
30
|
|
|
use Twig\Error\LoaderError; |
31
|
|
|
use verbb\supertable\fields\SuperTableField; |
|
|
|
|
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 */ |
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
|
|
|
|
41
|
|
|
* @author nystudio107 |
|
|
|
|
42
|
|
|
* @package ImageOptimize |
|
|
|
|
43
|
|
|
* @since 1.2.0 |
|
|
|
|
44
|
|
|
*/ |
|
|
|
|
45
|
|
|
class OptimizedImages extends Field |
46
|
|
|
{ |
47
|
|
|
// Constants |
48
|
|
|
// ========================================================================= |
49
|
|
|
|
50
|
|
|
const DEFAULT_ASPECT_RATIOS = [ |
51
|
|
|
['x' => 16, 'y' => 9], |
52
|
|
|
]; |
53
|
|
|
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
|
|
|
const MAX_VOLUME_SUBFOLDERS = 30; |
66
|
|
|
|
67
|
|
|
// Public Properties |
68
|
|
|
// ========================================================================= |
69
|
|
|
|
70
|
|
|
/** |
|
|
|
|
71
|
|
|
* @var array |
72
|
|
|
*/ |
73
|
|
|
public $fieldVolumeSettings = []; |
74
|
|
|
|
75
|
|
|
/** |
|
|
|
|
76
|
|
|
* @var array |
77
|
|
|
*/ |
78
|
|
|
public $ignoreFilesOfType = []; |
79
|
|
|
|
80
|
|
|
/** |
|
|
|
|
81
|
|
|
* @var bool |
82
|
|
|
*/ |
83
|
|
|
public $displayOptimizedImageVariants = true; |
84
|
|
|
|
85
|
|
|
/** |
|
|
|
|
86
|
|
|
* @var bool |
87
|
|
|
*/ |
88
|
|
|
public $displayDominantColorPalette = true; |
89
|
|
|
|
90
|
|
|
/** |
|
|
|
|
91
|
|
|
* @var bool |
92
|
|
|
*/ |
93
|
|
|
public $displayLazyLoadPlaceholderImages = true; |
94
|
|
|
|
95
|
|
|
/** |
|
|
|
|
96
|
|
|
* @var array |
97
|
|
|
*/ |
98
|
|
|
public $variants = []; |
99
|
|
|
|
100
|
|
|
// Private Properties |
101
|
|
|
// ========================================================================= |
102
|
|
|
|
103
|
|
|
/** |
|
|
|
|
104
|
|
|
* @var array |
105
|
|
|
*/ |
106
|
|
|
private $aspectRatios = []; |
|
|
|
|
107
|
|
|
|
108
|
|
|
// Static Methods |
109
|
|
|
// ========================================================================= |
110
|
|
|
|
111
|
|
|
/** |
|
|
|
|
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
|
|
|
/** |
|
|
|
|
127
|
|
|
* @inheritdoc |
128
|
|
|
*/ |
|
|
|
|
129
|
|
|
public static function displayName(): string |
130
|
|
|
{ |
131
|
|
|
return 'OptimizedImages'; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
|
|
|
|
135
|
|
|
* @inheritdoc |
136
|
|
|
*/ |
|
|
|
|
137
|
|
|
public function init() |
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) { |
|
|
|
|
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
|
|
|
/** |
|
|
|
|
162
|
|
|
* @inheritdoc |
163
|
|
|
*/ |
|
|
|
|
164
|
|
|
public function rules() |
165
|
|
|
{ |
166
|
|
|
$rules = parent::rules(); |
167
|
|
|
$rules = array_merge($rules, [ |
|
|
|
|
168
|
|
|
[ |
169
|
|
|
[ |
170
|
|
|
'displayOptimizedImageVariants', |
171
|
|
|
'displayDominantColorPalette', |
172
|
|
|
'displayLazyLoadPlaceholderImages', |
173
|
|
|
], |
174
|
|
|
'boolean', |
175
|
|
|
], |
176
|
|
|
[ |
177
|
|
|
[ |
178
|
|
|
'ignoreFilesOfType', |
179
|
|
|
'variants', |
180
|
|
|
], |
181
|
|
|
ArrayValidator::class |
182
|
|
|
], |
183
|
|
|
]); |
|
|
|
|
184
|
|
|
|
185
|
|
|
return $rules; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
|
|
|
|
189
|
|
|
* @inheritdoc |
190
|
|
|
* @since 1.6.2 |
|
|
|
|
191
|
|
|
*/ |
|
|
|
|
192
|
|
|
public function getContentGqlType() |
193
|
|
|
{ |
194
|
|
|
$typeArray = OptimizedImagesGenerator::generateTypes($this); |
195
|
|
|
|
196
|
|
|
return [ |
197
|
|
|
'name' => $this->handle, |
198
|
|
|
'description' => 'Optimized Images field', |
199
|
|
|
'type' => array_shift($typeArray), |
200
|
|
|
]; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
|
|
|
|
204
|
|
|
* @inheritdoc |
205
|
|
|
* @since 1.7.0 |
|
|
|
|
206
|
|
|
*/ |
|
|
|
|
207
|
|
|
public function useFieldset(): bool |
208
|
|
|
{ |
209
|
|
|
return true; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
|
|
|
|
213
|
|
|
* @inheritdoc |
214
|
|
|
*/ |
|
|
|
|
215
|
|
|
public function afterElementSave(ElementInterface $asset, bool $isNew) |
216
|
|
|
{ |
217
|
|
|
parent::afterElementSave($asset, $isNew); |
218
|
|
|
// Update our OptimizedImages Field data now that the Asset has been saved |
219
|
|
|
if ($asset !== null && $asset instanceof Asset && $asset->id !== null) { |
220
|
|
|
// If this element is propagating, we don't need to redo the image saving for each site |
221
|
|
|
if (!$asset->propagating) { |
222
|
|
|
// If the scenario is Asset::SCENARIO_FILEOPS or Asset::SCENARIO_MOVE (if using Craft > v3.7.1) treat it as a new asset |
223
|
|
|
$scenario = $asset->getScenario(); |
224
|
|
|
|
225
|
|
|
$supportsMoveScenario = version_compare( |
226
|
|
|
Craft::$app->getVersion(), |
|
|
|
|
227
|
|
|
'3.7.1', |
|
|
|
|
228
|
|
|
'>=' |
|
|
|
|
229
|
|
|
) === true; |
|
|
|
|
230
|
|
|
|
231
|
|
|
if ($isNew || $scenario === Asset::SCENARIO_FILEOPS || ($supportsMoveScenario && $scenario === Asset::SCENARIO_MOVE)) { |
232
|
|
|
/** |
233
|
|
|
* If this is a newly uploaded/created Asset, we can save the variants |
234
|
|
|
* via a queue job to prevent it from blocking |
235
|
|
|
*/ |
236
|
|
|
ImageOptimize::$plugin->optimizedImages->resaveAsset($asset->id); |
237
|
|
|
} else { |
238
|
|
|
/** |
239
|
|
|
* If it's not a newly uploaded/created Asset, check to see if the image |
240
|
|
|
* itself is being updated (via the ImageEditor). If so, update the |
241
|
|
|
* variants immediately so the AssetSelectorHud displays the new images |
242
|
|
|
*/ |
243
|
|
|
$request = Craft::$app->getRequest(); |
244
|
|
|
if (!$request->isConsoleRequest && $request->getPathInfo() === 'actions/assets/save-image') { |
245
|
|
|
try { |
246
|
|
|
ImageOptimize::$plugin->optimizedImages->updateOptimizedImageFieldData($this, $asset); |
247
|
|
|
} catch (Exception $e) { |
248
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
|
|
|
|
257
|
|
|
* @inheritdoc |
258
|
|
|
*/ |
|
|
|
|
259
|
|
|
public function normalizeValue($value, ElementInterface $asset = null) |
260
|
|
|
{ |
261
|
|
|
// If we're passed in a string, assume it's JSON-encoded, and decode it |
262
|
|
|
if (is_string($value) && !empty($value)) { |
263
|
|
|
$value = Json::decodeIfJson($value); |
264
|
|
|
} |
265
|
|
|
// If we're passed in an array, make a model from it |
266
|
|
|
if (is_array($value)) { |
267
|
|
|
// Create a new OptimizedImage model and populate it |
268
|
|
|
$model = new OptimizedImage($value); |
269
|
|
|
} elseif ($value instanceof OptimizedImage) { |
270
|
|
|
$model = $value; |
271
|
|
|
} else { |
272
|
|
|
// Just create a new empty model |
273
|
|
|
$model = new OptimizedImage(null); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
return $model; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
|
|
|
|
280
|
|
|
* @inheritdoc |
281
|
|
|
*/ |
|
|
|
|
282
|
|
|
public function getContentColumnType(): string |
283
|
|
|
{ |
284
|
|
|
return Schema::TYPE_TEXT; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
|
|
|
|
288
|
|
|
* @inheritdoc |
289
|
|
|
*/ |
|
|
|
|
290
|
|
|
public function getSettingsHtml() |
291
|
|
|
{ |
292
|
|
|
$namespace = Craft::$app->getView()->getNamespace(); |
293
|
|
|
if (strpos($namespace, Matrix::class) !== false || strpos($namespace, SuperTableField::class) !== false) { |
294
|
|
|
// Render an error template, since the field only works when attached to an Asset |
295
|
|
|
try { |
296
|
|
|
return Craft::$app->getView()->renderTemplate( |
297
|
|
|
'image-optimize/_components/fields/OptimizedImages_error', |
298
|
|
|
[ |
299
|
|
|
] |
300
|
|
|
); |
301
|
|
|
} catch (LoaderError $e) { |
302
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
303
|
|
|
} catch (\yii\base\Exception $e) { |
304
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
// Register our asset bundle |
308
|
|
|
try { |
309
|
|
|
Craft::$app->getView()->registerAssetBundle(ImageOptimizeAsset::class); |
310
|
|
|
} catch (InvalidConfigException $e) { |
311
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
try { |
315
|
|
|
$reflect = new ReflectionClass($this); |
316
|
|
|
$thisId = $reflect->getShortName(); |
317
|
|
|
} catch (ReflectionException $e) { |
318
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
319
|
|
|
$thisId = 0; |
320
|
|
|
} |
321
|
|
|
// Get our id and namespace |
322
|
|
|
if (ImageOptimize::$craft35) { |
323
|
|
|
$id = Html::id($thisId); |
324
|
|
|
} else { |
325
|
|
|
$id = Craft::$app->getView()->formatInputId($thisId); |
326
|
|
|
} |
327
|
|
|
$namespacedId = Craft::$app->getView()->namespaceInputId($id); |
328
|
|
|
$namespacePrefix = Craft::$app->getView()->namespaceInputName($thisId); |
329
|
|
|
$sizesWrapperId = Craft::$app->getView()->namespaceInputId('sizes-wrapper'); |
330
|
|
|
$view = Craft::$app->getView(); |
331
|
|
|
$view->registerJs( |
332
|
|
|
'new Craft.OptimizedImagesInput(' . |
333
|
|
|
'"' . $namespacedId . '", ' . |
334
|
|
|
'"' . $namespacePrefix . '",' . |
335
|
|
|
'"' . $sizesWrapperId . '"' . |
336
|
|
|
');' |
337
|
|
|
); |
338
|
|
|
|
339
|
|
|
// Prep our aspect ratios |
340
|
|
|
$aspectRatios = []; |
341
|
|
|
$index = 1; |
342
|
|
|
foreach ($this->aspectRatios as $aspectRatio) { |
343
|
|
|
if ($index % 6 === 0) { |
344
|
|
|
$aspectRatio['break'] = true; |
345
|
|
|
} |
346
|
|
|
$aspectRatios[] = $aspectRatio; |
347
|
|
|
$index++; |
348
|
|
|
} |
349
|
|
|
$aspectRatio = ['x' => 2, 'y' => 2, 'custom' => true]; |
350
|
|
|
$aspectRatios[] = $aspectRatio; |
351
|
|
|
// Get only the user-editable settings |
352
|
|
|
$settings = ImageOptimize::$plugin->getSettings(); |
353
|
|
|
|
354
|
|
|
// Render the settings template |
355
|
|
|
try { |
356
|
|
|
return Craft::$app->getView()->renderTemplate( |
357
|
|
|
'image-optimize/_components/fields/OptimizedImages_settings', |
358
|
|
|
[ |
359
|
|
|
'field' => $this, |
360
|
|
|
'settings' => $settings, |
361
|
|
|
'aspectRatios' => $aspectRatios, |
362
|
|
|
'id' => $id, |
363
|
|
|
'name' => $this->handle, |
364
|
|
|
'namespace' => $namespacedId, |
365
|
|
|
'fieldVolumes' => $this->getFieldVolumeInfo($this->handle), |
366
|
|
|
] |
367
|
|
|
); |
368
|
|
|
} catch (LoaderError $e) { |
369
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
370
|
|
|
} catch (\yii\base\Exception $e) { |
371
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
return ''; |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
|
|
|
|
378
|
|
|
* @inheritdoc |
379
|
|
|
*/ |
|
|
|
|
380
|
|
|
public function getInputHtml($value, ElementInterface $element = null): string |
381
|
|
|
{ |
382
|
|
|
if ($element !== null && $element instanceof Asset && $this->handle !== null) { |
383
|
|
|
/** @var Asset $element */ |
|
|
|
|
384
|
|
|
// Register our asset bundle |
385
|
|
|
try { |
386
|
|
|
Craft::$app->getView()->registerAssetBundle(ImageOptimizeAsset::class); |
387
|
|
|
} catch (InvalidConfigException $e) { |
388
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
// Get our id and namespace |
392
|
|
|
if (ImageOptimize::$craft35) { |
393
|
|
|
$id = Html::id($this->handle); |
394
|
|
|
} else { |
395
|
|
|
$id = Craft::$app->getView()->formatInputId($this->handle); |
396
|
|
|
} |
397
|
|
|
$nameSpaceId = Craft::$app->getView()->namespaceInputId($id); |
398
|
|
|
|
399
|
|
|
// Variables to pass down to our field JavaScript to let it namespace properly |
400
|
|
|
$jsonVars = [ |
401
|
|
|
'id' => $id, |
402
|
|
|
'name' => $this->handle, |
403
|
|
|
'namespace' => $nameSpaceId, |
404
|
|
|
'prefix' => Craft::$app->getView()->namespaceInputId(''), |
405
|
|
|
]; |
406
|
|
|
$jsonVars = Json::encode($jsonVars); |
407
|
|
|
$view = Craft::$app->getView(); |
408
|
|
|
$view->registerJs( |
409
|
|
|
"$('#{$nameSpaceId}-field').ImageOptimizeOptimizedImages(" . |
410
|
|
|
$jsonVars . |
411
|
|
|
");" |
412
|
|
|
); |
413
|
|
|
|
414
|
|
|
$settings = ImageOptimize::$plugin->getSettings(); |
415
|
|
|
$createVariants = ImageOptimize::$plugin->optimizedImages->shouldCreateVariants($this, $element); |
416
|
|
|
|
417
|
|
|
// Render the input template |
418
|
|
|
try { |
419
|
|
|
return Craft::$app->getView()->renderTemplate( |
420
|
|
|
'image-optimize/_components/fields/OptimizedImages_input', |
421
|
|
|
[ |
422
|
|
|
'name' => $this->handle, |
423
|
|
|
'value' => $value, |
424
|
|
|
'variants' => $this->variants, |
425
|
|
|
'field' => $this, |
426
|
|
|
'settings' => $settings, |
427
|
|
|
'elementId' => $element->id, |
428
|
|
|
'format' => $element->getExtension(), |
429
|
|
|
'id' => $id, |
430
|
|
|
'nameSpaceId' => $nameSpaceId, |
431
|
|
|
'createVariants' => $createVariants, |
432
|
|
|
] |
433
|
|
|
); |
434
|
|
|
} catch (LoaderError $e) { |
435
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
436
|
|
|
} catch (\yii\base\Exception $e) { |
437
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
438
|
|
|
} |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
// Render an error template, since the field only works when attached to an Asset |
442
|
|
|
try { |
443
|
|
|
return Craft::$app->getView()->renderTemplate( |
444
|
|
|
'image-optimize/_components/fields/OptimizedImages_error', |
445
|
|
|
[ |
446
|
|
|
] |
447
|
|
|
); |
448
|
|
|
} catch (LoaderError $e) { |
449
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
450
|
|
|
} catch (\yii\base\Exception $e) { |
451
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
return ''; |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
// Protected Methods |
458
|
|
|
// ========================================================================= |
459
|
|
|
|
460
|
|
|
/** |
461
|
|
|
* Returns an array of asset volumes and their sub-folders |
462
|
|
|
* |
463
|
|
|
* @param string|null $fieldHandle |
|
|
|
|
464
|
|
|
* |
465
|
|
|
* @return array |
466
|
|
|
* @throws InvalidConfigException |
467
|
|
|
*/ |
468
|
|
|
protected function getFieldVolumeInfo($fieldHandle): array |
469
|
|
|
{ |
470
|
|
|
$result = []; |
471
|
|
|
if ($fieldHandle !== null) { |
472
|
|
|
$volumes = Craft::$app->getVolumes()->getAllVolumes(); |
473
|
|
|
$assets = Craft::$app->getAssets(); |
474
|
|
|
foreach ($volumes as $volume) { |
475
|
|
|
if (is_subclass_of($volume, Volume::class)) { |
476
|
|
|
/** @var Volume $volume */ |
|
|
|
|
477
|
|
|
if ($this->volumeHasField($volume, $fieldHandle)) { |
478
|
|
|
$tree = $assets->getFolderTreeByVolumeIds([$volume->id]); |
479
|
|
|
$result[] = [ |
480
|
|
|
'name' => $volume->name, |
481
|
|
|
'handle' => $volume->handle, |
482
|
|
|
'subfolders' => $this->assembleSourceList($tree), |
483
|
|
|
]; |
484
|
|
|
} |
485
|
|
|
} |
486
|
|
|
} |
487
|
|
|
} |
488
|
|
|
// If there are too many sub-folders in an Asset volume, don't display them, return an empty array |
489
|
|
|
if (count($result) > self::MAX_VOLUME_SUBFOLDERS) { |
490
|
|
|
$result = []; |
491
|
|
|
} |
492
|
|
|
|
493
|
|
|
return $result; |
494
|
|
|
} |
495
|
|
|
|
496
|
|
|
/** |
497
|
|
|
* See if the passed $volume has an OptimizedImagesField with the handle $fieldHandle |
498
|
|
|
* |
499
|
|
|
* @param Volume $volume |
|
|
|
|
500
|
|
|
* |
501
|
|
|
* @param string $fieldHandle |
|
|
|
|
502
|
|
|
* |
503
|
|
|
* @return bool |
504
|
|
|
* @throws InvalidConfigException |
505
|
|
|
*/ |
506
|
|
|
protected function volumeHasField(Volume $volume, string $fieldHandle): bool |
507
|
|
|
{ |
508
|
|
|
$result = false; |
509
|
|
|
/** @var FieldLayout $fieldLayout */ |
|
|
|
|
510
|
|
|
$fieldLayout = $volume->getFieldLayout(); |
511
|
|
|
// Loop through the fields in the layout to see if there is an OptimizedImages field |
512
|
|
|
if ($fieldLayout) { |
|
|
|
|
513
|
|
|
$fields = $fieldLayout->getFields(); |
514
|
|
|
foreach ($fields as $field) { |
515
|
|
|
if ($field instanceof OptimizedImagesField && $field->handle === $fieldHandle) { |
516
|
|
|
$result = true; |
517
|
|
|
} |
518
|
|
|
} |
519
|
|
|
} |
520
|
|
|
|
521
|
|
|
return $result; |
522
|
|
|
} |
523
|
|
|
|
524
|
|
|
/** |
525
|
|
|
* Transforms an asset folder tree into a source list. |
526
|
|
|
* |
527
|
|
|
* @param array $folders |
|
|
|
|
528
|
|
|
* @param bool $includeNestedFolders |
|
|
|
|
529
|
|
|
* |
530
|
|
|
* @return array |
531
|
|
|
*/ |
532
|
|
|
protected function assembleSourceList(array $folders, bool $includeNestedFolders = true): array |
533
|
|
|
{ |
534
|
|
|
$sources = []; |
535
|
|
|
|
536
|
|
|
foreach ($folders as $folder) { |
537
|
|
|
$children = $folder->getChildren(); |
538
|
|
|
foreach ($children as $child) { |
539
|
|
|
$sources[$child->name] = $child->name; |
540
|
|
|
} |
541
|
|
|
} |
542
|
|
|
|
543
|
|
|
return $sources; |
544
|
|
|
} |
545
|
|
|
} |
546
|
|
|
|