1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Customer\Model\Customer\Attribute\Source;
/**
* Customer store attribute source
*
* @author Magento Core Team <core@magentocommerce.com>
*/
class Store extends \Magento\Eav\Model\Entity\Attribute\Source\Table
{
/**
* @var \Magento\Store\Model\System\Store
*/
protected $_store;
/**
* @var \Magento\Store\Model\ResourceModel\Store\CollectionFactory
*/
protected $_storesFactory;
/**
* @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory
* @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory $attrOptionFactory
* @param \Magento\Store\Model\System\Store $store
* @param \Magento\Store\Model\ResourceModel\Store\CollectionFactory $storesFactory
*/
public function __construct(
\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory,
\Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory $attrOptionFactory,
\Magento\Store\Model\System\Store $store,
\Magento\Store\Model\ResourceModel\Store\CollectionFactory $storesFactory
) {
parent::__construct($attrOptionCollectionFactory, $attrOptionFactory);
$this->_store = $store;
$this->_storesFactory = $storesFactory;
}
/**
* @inheritdoc
*/
public function getAllOptions($withEmpty = true, $defaultValues = false)
{
if (!$this->_options) {
$collection = $this->_createStoresCollection();
if ('store_id' == $this->getAttribute()->getAttributeCode()) {
$collection->setWithoutDefaultFilter();
}
$this->_options = $this->_store->getStoreValuesForForm();
if ('created_in' == $this->getAttribute()->getAttributeCode()) {
array_unshift($this->_options, ['value' => '0', 'label' => __('Admin')]);
}
}
return $this->_options;
}
/**
* @param string $value
* @return array|string
*/
public function getOptionText($value)
{
if (!$value) {
$value = '0';
}
$isMultiple = false;
if (strpos($value, ',') !== false) {
$isMultiple = true;
$value = explode(',', $value);
}
if (!$this->_options) {
$collection = $this->_createStoresCollection();
if ('store_id' == $this->getAttribute()->getAttributeCode()) {
$collection->setWithoutDefaultFilter();
}
$this->_options = $collection->load()->toOptionArray();
if ('created_in' == $this->getAttribute()->getAttributeCode()) {
array_unshift($this->_options, ['value' => '0', 'label' => __('Admin')]);
}
}
if ($isMultiple) {
$values = [];
foreach ($value as $val) {
$values[] = $this->_options[$val];
}
return $values;
} else {
return $this->_options[$value];
}
}
/**
* @return \Magento\Store\Model\ResourceModel\Store\Collection
*/
protected function _createStoresCollection()
{
return $this->_storesFactory->create();
}
}