Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
All Data Structures Namespaces Files Functions Variables Pages
Customer.php
Go to the documentation of this file.
1 <?php
7 
9 
15 class Customer extends \Magento\Framework\App\Config\Value
16 {
20  protected $_eavConfig;
21 
25  protected $storeManager;
26 
38  public function __construct(
39  \Magento\Framework\Model\Context $context,
40  \Magento\Framework\Registry $registry,
42  \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
44  \Magento\Eav\Model\Config $eavConfig,
45  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
46  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
47  array $data = []
48  ) {
49  $this->_eavConfig = $eavConfig;
50  parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
51  $this->storeManager = $storeManager;
52  }
53 
59  protected function _getAttributeCode()
60  {
61  return str_replace('_show', '', $this->getField());
62  }
63 
69  protected function _getAttributeObjects()
70  {
71  return [$this->_eavConfig->getAttribute('customer', $this->_getAttributeCode())];
72  }
73 
79  public function afterSave()
80  {
81  $result = parent::afterSave();
82 
83  $valueConfig = [
84  '' => ['is_required' => 0, 'is_visible' => 0],
85  'opt' => ['is_required' => 0, 'is_visible' => 1],
86  '1' => ['is_required' => 0, 'is_visible' => 1],
87  'req' => ['is_required' => 1, 'is_visible' => 1],
88  ];
89 
90  $value = $this->getValue();
91  if (isset($valueConfig[$value])) {
92  $data = $valueConfig[$value];
93  } else {
94  $data = $valueConfig[''];
95  }
96 
97  if ($this->getScope() == 'websites') {
98  $website = $this->storeManager->getWebsite($this->getScopeCode());
99  $dataFieldPrefix = 'scope_';
100  } else {
101  $website = null;
102  $dataFieldPrefix = '';
103  }
104 
105  foreach ($this->_getAttributeObjects() as $attributeObject) {
106  if ($website) {
107  $attributeObject->setWebsite($website);
108  $attributeObject->load($attributeObject->getId());
109  }
110  $attributeObject->setData($dataFieldPrefix . 'is_required', $data['is_required']);
111  $attributeObject->setData($dataFieldPrefix . 'is_visible', $data['is_visible']);
112  $attributeObject->save();
113  }
114 
115  return $result;
116  }
117 
123  public function afterDelete()
124  {
125  $result = parent::afterDelete();
126 
127  if ($this->getScope() == 'websites') {
128  $website = $this->storeManager->getWebsite($this->getScopeCode());
129  foreach ($this->_getAttributeObjects() as $attributeObject) {
130  $attributeObject->setWebsite($website);
131  $attributeObject->load($attributeObject->getId());
132  $attributeObject->setData('scope_is_required', null);
133  $attributeObject->setData('scope_is_visible', null);
134  $attributeObject->save();
135  }
136  }
137 
138  return $result;
139  }
140 }
$config
Definition: fraud_order.php:17
$resource
Definition: bulk.php:12
$value
Definition: gender.phtml:16
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\App\Config\ScopeConfigInterface $config, \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Eav\Model\Config $eavConfig, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
Definition: Customer.php:38