Skip to content

Commit

Permalink
Bring back CmsBlock source model.
Browse files Browse the repository at this point in the history
  • Loading branch information
vovayatsyuk committed Nov 19, 2020
1 parent b180f10 commit bf6ac9f
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions Model/Config/Source/CmsBlock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Swissup\Core\Model\Config\Source;

use Magento\Cms\Model\ResourceModel\Block\CollectionFactory;

class CmsBlock implements \Magento\Framework\Data\OptionSourceInterface
{
/**
* Block collection factory
*
* @var CollectionFactory
*/
protected $blockCollectionFactory;

/**
* Construct
*
* @param CollectionFactory $blockCollectionFactory
*/
public function __construct(CollectionFactory $blockCollectionFactory)
{
$this->blockCollectionFactory = $blockCollectionFactory;
}

/**
* Get options
*
* @return array
*/
public function toOptionArray()
{
$collection = $this->blockCollectionFactory->create()
->setOrder('title', 'ASC');

$options = [];
foreach ($collection as $block) {
$entry = [
'value' => $block->getId(),
'label' => $block->getTitle()
];

if (is_array($block->getStoreId()) && !in_array(0, $block->getStoreId())) {
$entry['label'] .= ' (' . implode(', ', $block->getStoreId()) . ')';
}

$options[] = $entry;
}

array_unshift($options, ['value' => '0', 'label' => __('No')]);

return $options;
}
}

0 comments on commit bf6ac9f

Please sign in to comment.