Skip to content

Commit 05b9b2b

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-56941' into BUGS
2 parents 7800e37 + fe86a0a commit 05b9b2b

File tree

25 files changed

+1376
-27
lines changed

25 files changed

+1376
-27
lines changed

app/code/Magento/Backend/etc/adminhtml/system.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@
198198
<resource>Magento_Config::config_general</resource>
199199
<group id="country" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
200200
<label>Country Options</label>
201-
<field id="allow" translate="label" type="multiselect" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
201+
<field id="allow" translate="label" type="multiselect" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="0" canRestore="1">
202202
<label>Allow Countries</label>
203203
<source_model>Magento\Directory\Model\Config\Source\Country</source_model>
204204
<can_be_empty>1</can_be_empty>

app/code/Magento/Customer/Model/Customer/DataProvider.php

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77

88
use Magento\Customer\Api\AddressMetadataInterface;
99
use Magento\Customer\Api\CustomerMetadataInterface;
10+
use Magento\Customer\Api\Data\AddressInterface;
11+
use Magento\Customer\Api\Data\CustomerInterface;
1012
use Magento\Customer\Model\Attribute;
1113
use Magento\Customer\Model\FileProcessor;
1214
use Magento\Customer\Model\FileProcessorFactory;
15+
use Magento\Customer\Model\ResourceModel\Address\Attribute\Source\CountryWithWebsites;
1316
use Magento\Eav\Api\Data\AttributeInterface;
1417
use Magento\Eav\Model\Config;
1518
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
@@ -55,6 +58,16 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
5558
*/
5659
protected $loadedData;
5760

61+
/**
62+
* @var CountryWithWebsites
63+
*/
64+
private $countryWithWebsiteSource;
65+
66+
/**
67+
* @var \Magento\Customer\Model\Config\Share
68+
*/
69+
private $shareConfig;
70+
5871
/**
5972
* EAV attribute properties to fetch from meta storage
6073
* @var array
@@ -117,6 +130,7 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
117130
* @param FileProcessorFactory $fileProcessorFactory
118131
* @param array $meta
119132
* @param array $data
133+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
120134
*/
121135
public function __construct(
122136
$name,
@@ -234,6 +248,7 @@ private function overrideFileUploaderData($entity, array &$entityData)
234248
* @param Attribute $attribute
235249
* @param array $customerData
236250
* @return array
251+
* @SuppressWarnings(PHPMD.NPathComplexity)
237252
*/
238253
private function getFileUploaderData(
239254
Type $entityType,
@@ -292,6 +307,7 @@ protected function getAttributesMeta(Type $entityType)
292307
$this->processFrontendInput($attribute, $meta);
293308

294309
$code = $attribute->getAttributeCode();
310+
295311
// use getDataUsingMethod, since some getters are defined and apply additional processing of returning value
296312
foreach ($this->metaProperties as $metaName => $origName) {
297313
$value = $attribute->getDataUsingMethod($origName);
@@ -304,7 +320,12 @@ protected function getAttributesMeta(Type $entityType)
304320
}
305321

306322
if ($attribute->usesSource()) {
307-
$meta[$code]['arguments']['data']['config']['options'] = $attribute->getSource()->getAllOptions();
323+
if ($code == AddressInterface::COUNTRY_ID) {
324+
$meta[$code]['arguments']['data']['config']['options'] = $this->getCountryWithWebsiteSource()
325+
->getAllOptions();
326+
} else {
327+
$meta[$code]['arguments']['data']['config']['options'] = $attribute->getSource()->getAllOptions();
328+
}
308329
}
309330

310331
$rules = $this->eavValidationRules->build($attribute, $meta[$code]['arguments']['data']['config']);
@@ -315,9 +336,61 @@ protected function getAttributesMeta(Type $entityType)
315336

316337
$this->overrideFileUploaderMetadata($entityType, $attribute, $meta[$code]['arguments']['data']['config']);
317338
}
339+
340+
$this->processWebsiteMeta($meta);
318341
return $meta;
319342
}
320343

344+
/**
345+
* Retrieve Country With Websites Source
346+
*
347+
* @deprecated
348+
* @return CountryWithWebsites
349+
*/
350+
private function getCountryWithWebsiteSource()
351+
{
352+
if (!$this->countryWithWebsiteSource) {
353+
$this->countryWithWebsiteSource = ObjectManager::getInstance()->get(CountryWithWebsites::class);
354+
}
355+
356+
return $this->countryWithWebsiteSource;
357+
}
358+
359+
/**
360+
* Retrieve Customer Config Share
361+
*
362+
* @deprecated
363+
* @return \Magento\Customer\Model\Config\Share
364+
*/
365+
private function getShareConfig()
366+
{
367+
if (!$this->shareConfig) {
368+
$this->shareConfig = ObjectManager::getInstance()->get(\Magento\Customer\Model\Config\Share::class);
369+
}
370+
371+
return $this->shareConfig;
372+
}
373+
374+
/**
375+
* Add global scope parameter and filter options to website meta
376+
*
377+
* @param array $meta
378+
* @return void
379+
*/
380+
private function processWebsiteMeta(&$meta)
381+
{
382+
if (isset($meta[CustomerInterface::WEBSITE_ID]) && $this->getShareConfig()->isGlobalScope()) {
383+
$meta[CustomerInterface::WEBSITE_ID]['arguments']['data']['config']['isGlobalScope'] = 1;
384+
}
385+
386+
if (isset($meta[AddressInterface::COUNTRY_ID]) && !$this->getShareConfig()->isGlobalScope()) {
387+
$meta[AddressInterface::COUNTRY_ID]['arguments']['data']['config']['filterBy'] = [
388+
'target' => '${ $.provider }:data.customer.website_id',
389+
'field' => 'website_ids'
390+
];
391+
}
392+
}
393+
321394
/**
322395
* Override file uploader UI component metadata
323396
*
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Customer\Model\Plugin;
8+
9+
use Magento\Customer\Model\Config\Share;
10+
use Magento\Store\Api\Data\WebsiteInterface;
11+
use Magento\Store\Model\ScopeInterface;
12+
use Magento\Store\Model\StoreManagerInterface;
13+
14+
/**
15+
* Class AllowedCountries
16+
*/
17+
class AllowedCountries
18+
{
19+
/**
20+
* @var \Magento\Customer\Model\Config\Share
21+
*/
22+
private $shareConfig;
23+
24+
/**
25+
* @var StoreManagerInterface
26+
*/
27+
private $storeManager;
28+
29+
/**
30+
* @param Share $share
31+
* @param StoreManagerInterface $storeManager
32+
*/
33+
public function __construct(
34+
Share $share,
35+
StoreManagerInterface $storeManager
36+
) {
37+
$this->shareConfig = $share;
38+
$this->storeManager = $storeManager;
39+
}
40+
41+
/**
42+
* Retrieve all allowed countries or specific by scope depends on customer share setting
43+
*
44+
* @param \Magento\Directory\Model\AllowedCountries $subject
45+
* @param string | null $filter
46+
* @param string $scope
47+
*/
48+
public function beforeGetAllowedCountries(
49+
\Magento\Directory\Model\AllowedCountries $subject,
50+
$scope = ScopeInterface::SCOPE_WEBSITE,
51+
$scopeCode = null
52+
) {
53+
if ($this->shareConfig->isGlobalScope()) {
54+
//Check if we have shared accounts - than merge all website allowed countries
55+
$scopeCode = array_map(function (WebsiteInterface $website) {
56+
return $website->getId();
57+
}, $this->storeManager->getWebsites());
58+
$scope = ScopeInterface::SCOPE_WEBSITES;
59+
}
60+
61+
return [$scope, $scopeCode];
62+
}
63+
}

app/code/Magento/Customer/Model/ResourceModel/Address/Attribute/Source/Country.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,27 @@
1111
*/
1212
namespace Magento\Customer\Model\ResourceModel\Address\Attribute\Source;
1313

14+
use Magento\Checkout\Model\Session;
15+
use Magento\Framework\App\ObjectManager;
16+
use Magento\Store\Api\StoreResolverInterface;
17+
use Magento\Store\Model\StoreManagerInterface;
18+
19+
/**
20+
* Class Country.
21+
* @package Magento\Customer\Model\ResourceModel\Address\Attribute\Source
22+
*/
1423
class Country extends \Magento\Eav\Model\Entity\Attribute\Source\Table
1524
{
1625
/**
1726
* @var \Magento\Directory\Model\ResourceModel\Country\CollectionFactory
1827
*/
1928
protected $_countriesFactory;
2029

30+
/**
31+
* @var StoreResolverInterface
32+
*/
33+
private $storeResolver;
34+
2135
/**
2236
* @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory
2337
* @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory $attrOptionFactory
@@ -41,7 +55,7 @@ public function getAllOptions()
4155
{
4256
if (!$this->_options) {
4357
$this->_options = $this->_createCountriesCollection()->loadByStore(
44-
$this->getAttribute()->getStoreId()
58+
$this->getStoreResolver()->getCurrentStoreId()
4559
)->toOptionArray();
4660
}
4761
return $this->_options;
@@ -54,4 +68,18 @@ protected function _createCountriesCollection()
5468
{
5569
return $this->_countriesFactory->create();
5670
}
71+
72+
/**
73+
* Retrieve Store Resolver
74+
* @deprecated
75+
* @return StoreResolverInterface
76+
*/
77+
private function getStoreResolver()
78+
{
79+
if (!$this->storeResolver) {
80+
$this->storeResolver = ObjectManager::getInstance()->get(StoreResolverInterface::class);
81+
}
82+
83+
return $this->storeResolver;
84+
}
5785
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
/**
8+
* Customer country with website specified attribute source
9+
*
10+
* @author Magento Core Team <core@magentocommerce.com>
11+
*/
12+
namespace Magento\Customer\Model\ResourceModel\Address\Attribute\Source;
13+
14+
use Magento\Customer\Model\Config\Share;
15+
use Magento\Directory\Model\AllowedCountries;
16+
use Magento\Store\Model\ScopeInterface;
17+
18+
class CountryWithWebsites extends \Magento\Eav\Model\Entity\Attribute\Source\Table
19+
{
20+
/**
21+
* @var \Magento\Directory\Model\ResourceModel\Country\CollectionFactory
22+
*/
23+
private $countriesFactory;
24+
25+
/**
26+
* @var \Magento\Directory\Model\AllowedCountries
27+
*/
28+
private $allowedCountriesReader;
29+
30+
/**
31+
* @var array
32+
*/
33+
private $options;
34+
35+
/**
36+
* @var \Magento\Store\Model\StoreManagerInterface
37+
*/
38+
private $storeManager;
39+
40+
/**
41+
* @var Share
42+
*/
43+
private $shareConfig;
44+
45+
/**
46+
* @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory
47+
* @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory $attrOptionFactory
48+
* @param \Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countriesFactory
49+
* @param AllowedCountries $allowedCountriesReader
50+
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
51+
*/
52+
public function __construct(
53+
\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory,
54+
\Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory $attrOptionFactory,
55+
\Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countriesFactory,
56+
\Magento\Directory\Model\AllowedCountries $allowedCountriesReader,
57+
\Magento\Store\Model\StoreManagerInterface $storeManager,
58+
\Magento\Customer\Model\Config\Share $shareConfig
59+
) {
60+
$this->countriesFactory = $countriesFactory;
61+
$this->allowedCountriesReader = $allowedCountriesReader;
62+
$this->storeManager = $storeManager;
63+
$this->shareConfig = $shareConfig;
64+
parent::__construct($attrOptionCollectionFactory, $attrOptionFactory);
65+
}
66+
67+
/**
68+
* Retrieve all options
69+
*
70+
* @return array
71+
*/
72+
public function getAllOptions()
73+
{
74+
if (!$this->options) {
75+
$allowedCountries = [];
76+
$websiteIds = [];
77+
78+
if (!$this->shareConfig->isGlobalScope()) {
79+
foreach ($this->storeManager->getWebsites() as $website) {
80+
$countries = $this->allowedCountriesReader
81+
->getAllowedCountries(ScopeInterface::SCOPE_WEBSITE, $website->getId());
82+
$allowedCountries = array_merge($allowedCountries, $countries);
83+
84+
foreach ($countries as $countryCode) {
85+
$websiteIds[$countryCode][] = $website->getId();
86+
}
87+
}
88+
} else {
89+
$allowedCountries = $this->allowedCountriesReader->getAllowedCountries();
90+
}
91+
92+
93+
$this->options = $this->createCountriesCollection()
94+
->addFieldToFilter('country_id', ['in' => $allowedCountries])
95+
->toOptionArray();
96+
97+
foreach ($this->options as &$option) {
98+
if (isset($websiteIds[$option['value']])) {
99+
$option['website_ids'] = $websiteIds[$option['value']];
100+
}
101+
}
102+
}
103+
104+
return $this->options;
105+
}
106+
107+
/**
108+
* Create Countries Collection with all countries
109+
*
110+
* @return \Magento\Directory\Model\ResourceModel\Country\Collection
111+
*/
112+
private function createCountriesCollection()
113+
{
114+
return $this->countriesFactory->create();
115+
}
116+
}

0 commit comments

Comments
 (0)