40 lines
1.2 KiB
PHP
40 lines
1.2 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Esun\PromotionPopup\Model\Config\Source;
|
|
|
|
use Magento\Cms\Model\ResourceModel\Block\CollectionFactory;
|
|
use Magento\Framework\Data\OptionSourceInterface;
|
|
|
|
class CmsBlock implements OptionSourceInterface
|
|
{
|
|
public function __construct(private readonly CollectionFactory $collectionFactory)
|
|
{
|
|
}
|
|
|
|
public function toOptionArray(): array
|
|
{
|
|
$collection = $this->collectionFactory->create();
|
|
$collection->addFieldToSelect(['title', 'identifier']);
|
|
$collection->addFieldToFilter('is_active', 1);
|
|
$collection->setOrder('title', 'ASC');
|
|
|
|
$options = [['value' => '', 'label' => __('-- Please Select --')]];
|
|
$identifiers = [];
|
|
foreach ($collection as $block) {
|
|
$identifier = trim((string)$block->getIdentifier());
|
|
if ($identifier === '' || isset($identifiers[$identifier])) {
|
|
continue;
|
|
}
|
|
|
|
$identifiers[$identifier] = true;
|
|
$options[] = [
|
|
'value' => $identifier,
|
|
'label' => sprintf('%s [%s]', (string)$block->getTitle(), $identifier),
|
|
];
|
|
}
|
|
|
|
return $options;
|
|
}
|
|
}
|