Skip to content

Commit

Permalink
add block config source
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-kl1 committed Jun 11, 2018
1 parent 38d7977 commit 31e6bc2
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 28 deletions.
46 changes: 46 additions & 0 deletions app/code/Magento/Cms/Model/Config/Source/Block.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Cms\Model\Config\Source;

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

/**
* Class Block
*/
class Block implements \Magento\Framework\Option\ArrayInterface
{
/**
* @var array
*/
private $options;

/**
* @var CollectionFactory
*/
private $collectionFactory;

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

/**
* To option array
*
* @return array
*/
public function toOptionArray()
{
if (!$this->options) {
$this->options = $this->collectionFactory->create()->toOptionIdArray();
}
return $this->options;
}
}
28 changes: 28 additions & 0 deletions app/code/Magento/Cms/Model/ResourceModel/AbstractCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,32 @@ public function getSelectCountSql()

return $countSelect;
}

/**
* Returns pairs identifier - title for unique identifiers
* and pairs identifier|entity_id - title for non-unique after first
*
* @return array
*/
public function toOptionIdArray()
{
$res = [];
$existingIdentifiers = [];
foreach ($this as $item) {
$identifier = $item->getData('identifier');

$data['value'] = $identifier;
$data['label'] = $item->getData('title');

if (in_array($identifier, $existingIdentifiers)) {
$data['value'] .= '|' . $item->getData($this->getIdFieldName());
} else {
$existingIdentifiers[] = $identifier;
}

$res[] = $data;
}

return $res;
}
}
28 changes: 0 additions & 28 deletions app/code/Magento/Cms/Model/ResourceModel/Page/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,34 +51,6 @@ protected function _construct()
$this->_map['fields']['store'] = 'store_table.store_id';
}

/**
* Returns pairs identifier - title for unique identifiers
* and pairs identifier|page_id - title for non-unique after first
*
* @return array
*/
public function toOptionIdArray()
{
$res = [];
$existingIdentifiers = [];
foreach ($this as $item) {
$identifier = $item->getData('identifier');

$data['value'] = $identifier;
$data['label'] = $item->getData('title');

if (in_array($identifier, $existingIdentifiers)) {
$data['value'] .= '|' . $item->getData('page_id');
} else {
$existingIdentifiers[] = $identifier;
}

$res[] = $data;
}

return $res;
}

/**
* Set first store flag
*
Expand Down

0 comments on commit 31e6bc2

Please sign in to comment.