Skip to content

Commit a971cc7

Browse files
author
Yaroslav Onischenko
authored
Merge pull request #702 from magento-mpi/MAGETWO-62669
[MPI] Fix of payment methods admin panel extensibility
2 parents 30032a0 + 4e98feb commit a971cc7

File tree

4 files changed

+489
-51
lines changed

4 files changed

+489
-51
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Paypal\Model\Config\Structure;
7+
8+
/**
9+
* PayPal change structure of payment methods configuration in admin panel.
10+
*/
11+
class PaymentSectionModifier
12+
{
13+
/**
14+
* Identifiers of special payment method configuration groups
15+
*
16+
* @var array
17+
*/
18+
private static $specialGroups = [
19+
'account',
20+
'recommended_solutions',
21+
'other_paypal_payment_solutions',
22+
'other_payment_methods',
23+
];
24+
25+
/**
26+
* Returns changed section structure.
27+
*
28+
* Payment configuration has predefined special blocks:
29+
* - Account information (id = account),
30+
* - Recommended Solutions (id = recommended_solutions),
31+
* - Other PayPal paymnt solution (id = other_paypal_payment_solutions),
32+
* - Other payment methods (id = other_payment_methods).
33+
* All payment methods configuration should be moved to one of this group.
34+
* To move payment method to specific configuration group specify "displayIn"
35+
* attribute in system.xml file equals to any id of predefined special group.
36+
* If "displayIn" attribute is not specified then payment method moved to "Other payment methods" group
37+
*
38+
* @param array $initialStructure
39+
* @return array
40+
*/
41+
public function modify(array $initialStructure)
42+
{
43+
$changedStructure = array_fill_keys(self::$specialGroups, []);
44+
45+
foreach ($initialStructure as $childSection => $childData) {
46+
if (in_array($childSection, self::$specialGroups)) {
47+
if (isset($changedStructure[$childSection]['children'])) {
48+
$children = $changedStructure[$childSection]['children'];
49+
if (isset($childData['children'])) {
50+
$children += $childData['children'];
51+
}
52+
$childData['children'] = $children;
53+
unset($children);
54+
}
55+
$changedStructure[$childSection] = $childData;
56+
} else {
57+
$moveInstructions = $this->getMoveInstructions($childSection, $childData);
58+
if (!empty($moveInstructions)) {
59+
foreach ($moveInstructions as $moveInstruction) {
60+
unset($childData['children'][$moveInstruction['section']]);
61+
unset($moveInstruction['data']['displayIn']);
62+
$changedStructure
63+
[$moveInstruction['parent']]
64+
['children']
65+
[$moveInstruction['section']] = $moveInstruction['data'];
66+
}
67+
}
68+
if (!isset($moveInstructions[$childSection])) {
69+
$changedStructure['other_payment_methods']['children'][$childSection] = $childData;
70+
}
71+
}
72+
}
73+
74+
return $changedStructure;
75+
}
76+
77+
/**
78+
* Recursively collect groups that should be moved to special section
79+
*
80+
* @param string $section
81+
* @param array $data
82+
* @return array
83+
*/
84+
private function getMoveInstructions($section, $data)
85+
{
86+
$moved = [];
87+
88+
if (array_key_exists('children', $data)) {
89+
foreach ($data['children'] as $childSection => $childData) {
90+
$movedChildren = $this->getMoveInstructions($childSection, $childData);
91+
if (isset($movedChildren[$childSection])) {
92+
unset($data['children'][$childSection]);
93+
}
94+
$moved = array_merge($moved, $movedChildren);
95+
}
96+
}
97+
98+
if (isset($data['displayIn']) && in_array($data['displayIn'], self::$specialGroups)) {
99+
$moved = array_merge(
100+
[
101+
$section => [
102+
'parent' => $data['displayIn'],
103+
'section' => $section,
104+
'data' => $data
105+
]
106+
],
107+
$moved
108+
);
109+
}
110+
111+
return $moved;
112+
}
113+
}

app/code/Magento/Paypal/Model/Config/StructurePlugin.php

Lines changed: 16 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
use Magento\Config\Model\Config\Structure;
1010
use Magento\Config\Model\Config\Structure\Element\Section;
1111
use Magento\Config\Model\Config\Structure\ElementInterface;
12+
use Magento\Framework\App\ObjectManager;
1213
use Magento\Paypal\Helper\Backend as BackendHelper;
14+
use Magento\Paypal\Model\Config\Structure\PaymentSectionModifier;
1315

1416
class StructurePlugin
1517
{
@@ -28,6 +30,11 @@ class StructurePlugin
2830
*/
2931
protected $_scopeDefiner;
3032

33+
/**
34+
* @var PaymentSectionModifier
35+
*/
36+
private $paymentSectionModifier;
37+
3138
/**
3239
* @var string[]
3340
*/
@@ -51,10 +58,13 @@ class StructurePlugin
5158
*/
5259
public function __construct(
5360
ScopeDefiner $scopeDefiner,
54-
BackendHelper $helper
61+
BackendHelper $helper,
62+
PaymentSectionModifier $paymentSectionModifier = null
5563
) {
5664
$this->_scopeDefiner = $scopeDefiner;
5765
$this->_helper = $helper;
66+
$this->paymentSectionModifier = $paymentSectionModifier
67+
?: ObjectManager::getInstance()->get(PaymentSectionModifier::class);
5868
}
5969

6070
/**
@@ -111,60 +121,16 @@ public function aroundGetElementByPathParts(
111121

112122
/**
113123
* Changes payment config structure.
114-
* Groups which have `displayIn` element, transfer to appropriate group.
115-
* Groups without `displayIn` transfer to other payment methods group.
116124
*
117125
* @param Section $result
118126
* @return void
119127
*/
120128
private function restructurePayments(Section $result)
121129
{
122-
$sectionMap = [
123-
'account' => [],
124-
'recommended_solutions' => [],
125-
'other_paypal_payment_solutions' => [],
126-
'other_payment_methods' => []
127-
];
128-
129-
$configuration = $result->getData();
130-
131-
foreach ($configuration['children'] as $section => $data) {
132-
if (array_key_exists($section, $sectionMap)) {
133-
$sectionMap[$section] = $data;
134-
} elseif ($displayIn = $this->getDisplayInSection($section, $data)) {
135-
$sectionMap[$displayIn['parent']]['children'][$displayIn['section']] = $displayIn['data'];
136-
} else {
137-
$sectionMap['other_payment_methods']['children'][$section] = $data;
138-
}
139-
}
140-
141-
$configuration['children'] = $sectionMap;
142-
$result->setData($configuration, $this->_scopeDefiner->getScope());
143-
}
144-
145-
/**
146-
* Recursive search of `displayIn` element in node children
147-
*
148-
* @param string $section
149-
* @param array $data
150-
* @return array|null
151-
*/
152-
private function getDisplayInSection($section, $data)
153-
{
154-
if (is_array($data) && array_key_exists('displayIn', $data)) {
155-
return [
156-
'parent' => $data['displayIn'],
157-
'section' => $section,
158-
'data' => $data
159-
];
160-
}
161-
162-
if (array_key_exists('children', $data)) {
163-
foreach ($data['children'] as $childSection => $childData) {
164-
return $this->getDisplayInSection($childSection, $childData);
165-
}
166-
}
167-
168-
return null;
130+
$sectionData = $result->getData();
131+
$sectionInitialStructure = isset($sectionData['children']) ? $sectionData['children'] : [];
132+
$sectionChangedStructure = $this->paymentSectionModifier->modify($sectionInitialStructure);
133+
$sectionData['children'] = $sectionChangedStructure;
134+
$result->setData($sectionData, $this->_scopeDefiner->getScope());
169135
}
170136
}

0 commit comments

Comments
 (0)