58 lines
1.7 KiB
PHP
58 lines
1.7 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Esun\Homepage\Model\Config\Backend;
|
|
|
|
use Magento\Config\Model\Config\Backend\Serialized\ArraySerialized;
|
|
use Magento\Config\Model\ResourceModel\Config\Data as ConfigResource;
|
|
use Magento\Config\Model\ResourceModel\Config\Data\Collection as ConfigCollection;
|
|
|
|
class CreateAnythingItems extends ArraySerialized
|
|
{
|
|
protected function _construct(): void
|
|
{
|
|
$this->_setResourceModel(ConfigResource::class, ConfigCollection::class);
|
|
}
|
|
|
|
public function beforeSave()
|
|
{
|
|
$value = $this->getValue();
|
|
$items = [];
|
|
|
|
if (is_array($value)) {
|
|
unset($value['__empty']);
|
|
foreach ($value as $row) {
|
|
if (!is_array($row)) {
|
|
continue;
|
|
}
|
|
|
|
$title = trim((string) ($row['title'] ?? ''));
|
|
$subtitle = trim((string) ($row['subtitle'] ?? ''));
|
|
$videoId = (int) ($row['video_id'] ?? 0);
|
|
$productSku = trim((string) ($row['product_sku'] ?? ''));
|
|
$sortNumber = (int) ($row['sort_number'] ?? 0);
|
|
|
|
if ($title === '' || $videoId <= 0 || $productSku === '') {
|
|
continue;
|
|
}
|
|
|
|
$items[] = [
|
|
'title' => $title,
|
|
'subtitle' => $subtitle,
|
|
'video_id' => $videoId,
|
|
'product_sku' => $productSku,
|
|
'sort_number' => $sortNumber,
|
|
];
|
|
}
|
|
}
|
|
|
|
usort($items, static function (array $left, array $right): int {
|
|
return $left['sort_number'] <=> $right['sort_number'];
|
|
});
|
|
|
|
$this->setValue($items);
|
|
|
|
return parent::beforeSave();
|
|
}
|
|
}
|