Skip to content

Commit e735178

Browse files
committed
add block config source
1 parent 05f9df7 commit e735178

File tree

3 files changed

+74
-28
lines changed

3 files changed

+74
-28
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Cms\Model\Config\Source;
7+
8+
use Magento\Cms\Model\ResourceModel\Block\CollectionFactory;
9+
10+
/**
11+
* Class Block
12+
*/
13+
class Block implements \Magento\Framework\Option\ArrayInterface
14+
{
15+
/**
16+
* @var array
17+
*/
18+
private $options;
19+
20+
/**
21+
* @var CollectionFactory
22+
*/
23+
private $collectionFactory;
24+
25+
/**
26+
* @param CollectionFactory $collectionFactory
27+
*/
28+
public function __construct(
29+
CollectionFactory $collectionFactory
30+
) {
31+
$this->collectionFactory = $collectionFactory;
32+
}
33+
34+
/**
35+
* To option array
36+
*
37+
* @return array
38+
*/
39+
public function toOptionArray()
40+
{
41+
if (!$this->options) {
42+
$this->options = $this->collectionFactory->create()->toOptionIdArray();
43+
}
44+
return $this->options;
45+
}
46+
}

app/code/Magento/Cms/Model/ResourceModel/AbstractCollection.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,32 @@ public function getSelectCountSql()
176176

177177
return $countSelect;
178178
}
179+
180+
/**
181+
* Returns pairs identifier - title for unique identifiers
182+
* and pairs identifier|entity_id - title for non-unique after first
183+
*
184+
* @return array
185+
*/
186+
public function toOptionIdArray()
187+
{
188+
$res = [];
189+
$existingIdentifiers = [];
190+
foreach ($this as $item) {
191+
$identifier = $item->getData('identifier');
192+
193+
$data['value'] = $identifier;
194+
$data['label'] = $item->getData('title');
195+
196+
if (in_array($identifier, $existingIdentifiers)) {
197+
$data['value'] .= '|' . $item->getData($this->getIdFieldName());
198+
} else {
199+
$existingIdentifiers[] = $identifier;
200+
}
201+
202+
$res[] = $data;
203+
}
204+
205+
return $res;
206+
}
179207
}

app/code/Magento/Cms/Model/ResourceModel/Page/Collection.php

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -51,34 +51,6 @@ protected function _construct()
5151
$this->_map['fields']['store'] = 'store_table.store_id';
5252
}
5353

54-
/**
55-
* Returns pairs identifier - title for unique identifiers
56-
* and pairs identifier|page_id - title for non-unique after first
57-
*
58-
* @return array
59-
*/
60-
public function toOptionIdArray()
61-
{
62-
$res = [];
63-
$existingIdentifiers = [];
64-
foreach ($this as $item) {
65-
$identifier = $item->getData('identifier');
66-
67-
$data['value'] = $identifier;
68-
$data['label'] = $item->getData('title');
69-
70-
if (in_array($identifier, $existingIdentifiers)) {
71-
$data['value'] .= '|' . $item->getData('page_id');
72-
} else {
73-
$existingIdentifiers[] = $identifier;
74-
}
75-
76-
$res[] = $data;
77-
}
78-
79-
return $res;
80-
}
81-
8254
/**
8355
* Set first store flag
8456
*

0 commit comments

Comments
 (0)