Skip to content

Commit

Permalink
Merge pull request #702 from magento-mpi/MAGETWO-62669
Browse files Browse the repository at this point in the history
[MPI] Fix of payment methods admin panel extensibility
  • Loading branch information
Yaroslav Onischenko authored Dec 28, 2016
2 parents 30032a0 + 4e98feb commit a971cc7
Show file tree
Hide file tree
Showing 4 changed files with 489 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Paypal\Model\Config\Structure;

/**
* PayPal change structure of payment methods configuration in admin panel.
*/
class PaymentSectionModifier
{
/**
* Identifiers of special payment method configuration groups
*
* @var array
*/
private static $specialGroups = [
'account',
'recommended_solutions',
'other_paypal_payment_solutions',
'other_payment_methods',
];

/**
* Returns changed section structure.
*
* Payment configuration has predefined special blocks:
* - Account information (id = account),
* - Recommended Solutions (id = recommended_solutions),
* - Other PayPal paymnt solution (id = other_paypal_payment_solutions),
* - Other payment methods (id = other_payment_methods).
* All payment methods configuration should be moved to one of this group.
* To move payment method to specific configuration group specify "displayIn"
* attribute in system.xml file equals to any id of predefined special group.
* If "displayIn" attribute is not specified then payment method moved to "Other payment methods" group
*
* @param array $initialStructure
* @return array
*/
public function modify(array $initialStructure)
{
$changedStructure = array_fill_keys(self::$specialGroups, []);

foreach ($initialStructure as $childSection => $childData) {
if (in_array($childSection, self::$specialGroups)) {
if (isset($changedStructure[$childSection]['children'])) {
$children = $changedStructure[$childSection]['children'];
if (isset($childData['children'])) {
$children += $childData['children'];
}
$childData['children'] = $children;
unset($children);
}
$changedStructure[$childSection] = $childData;
} else {
$moveInstructions = $this->getMoveInstructions($childSection, $childData);
if (!empty($moveInstructions)) {
foreach ($moveInstructions as $moveInstruction) {
unset($childData['children'][$moveInstruction['section']]);
unset($moveInstruction['data']['displayIn']);
$changedStructure
[$moveInstruction['parent']]
['children']
[$moveInstruction['section']] = $moveInstruction['data'];
}
}
if (!isset($moveInstructions[$childSection])) {
$changedStructure['other_payment_methods']['children'][$childSection] = $childData;
}
}
}

return $changedStructure;
}

/**
* Recursively collect groups that should be moved to special section
*
* @param string $section
* @param array $data
* @return array
*/
private function getMoveInstructions($section, $data)
{
$moved = [];

if (array_key_exists('children', $data)) {
foreach ($data['children'] as $childSection => $childData) {
$movedChildren = $this->getMoveInstructions($childSection, $childData);
if (isset($movedChildren[$childSection])) {
unset($data['children'][$childSection]);
}
$moved = array_merge($moved, $movedChildren);
}
}

if (isset($data['displayIn']) && in_array($data['displayIn'], self::$specialGroups)) {
$moved = array_merge(
[
$section => [
'parent' => $data['displayIn'],
'section' => $section,
'data' => $data
]
],
$moved
);
}

return $moved;
}
}
66 changes: 16 additions & 50 deletions app/code/Magento/Paypal/Model/Config/StructurePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
use Magento\Config\Model\Config\Structure;
use Magento\Config\Model\Config\Structure\Element\Section;
use Magento\Config\Model\Config\Structure\ElementInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Paypal\Helper\Backend as BackendHelper;
use Magento\Paypal\Model\Config\Structure\PaymentSectionModifier;

class StructurePlugin
{
Expand All @@ -28,6 +30,11 @@ class StructurePlugin
*/
protected $_scopeDefiner;

/**
* @var PaymentSectionModifier
*/
private $paymentSectionModifier;

/**
* @var string[]
*/
Expand All @@ -51,10 +58,13 @@ class StructurePlugin
*/
public function __construct(
ScopeDefiner $scopeDefiner,
BackendHelper $helper
BackendHelper $helper,
PaymentSectionModifier $paymentSectionModifier = null
) {
$this->_scopeDefiner = $scopeDefiner;
$this->_helper = $helper;
$this->paymentSectionModifier = $paymentSectionModifier
?: ObjectManager::getInstance()->get(PaymentSectionModifier::class);
}

/**
Expand Down Expand Up @@ -111,60 +121,16 @@ public function aroundGetElementByPathParts(

/**
* Changes payment config structure.
* Groups which have `displayIn` element, transfer to appropriate group.
* Groups without `displayIn` transfer to other payment methods group.
*
* @param Section $result
* @return void
*/
private function restructurePayments(Section $result)
{
$sectionMap = [
'account' => [],
'recommended_solutions' => [],
'other_paypal_payment_solutions' => [],
'other_payment_methods' => []
];

$configuration = $result->getData();

foreach ($configuration['children'] as $section => $data) {
if (array_key_exists($section, $sectionMap)) {
$sectionMap[$section] = $data;
} elseif ($displayIn = $this->getDisplayInSection($section, $data)) {
$sectionMap[$displayIn['parent']]['children'][$displayIn['section']] = $displayIn['data'];
} else {
$sectionMap['other_payment_methods']['children'][$section] = $data;
}
}

$configuration['children'] = $sectionMap;
$result->setData($configuration, $this->_scopeDefiner->getScope());
}

/**
* Recursive search of `displayIn` element in node children
*
* @param string $section
* @param array $data
* @return array|null
*/
private function getDisplayInSection($section, $data)
{
if (is_array($data) && array_key_exists('displayIn', $data)) {
return [
'parent' => $data['displayIn'],
'section' => $section,
'data' => $data
];
}

if (array_key_exists('children', $data)) {
foreach ($data['children'] as $childSection => $childData) {
return $this->getDisplayInSection($childSection, $childData);
}
}

return null;
$sectionData = $result->getData();
$sectionInitialStructure = isset($sectionData['children']) ? $sectionData['children'] : [];
$sectionChangedStructure = $this->paymentSectionModifier->modify($sectionInitialStructure);
$sectionData['children'] = $sectionChangedStructure;
$result->setData($sectionData, $this->_scopeDefiner->getScope());
}
}
Loading

0 comments on commit a971cc7

Please sign in to comment.