Skip to content

Commit

Permalink
Merge pull request #1410 from magento-engcom/MSI-1250-Prevent-Disabli…
Browse files Browse the repository at this point in the history
…ng-Default-Source-by-Disabling-Is-Enabled-Switcher

1250: added plugin to prevent disabling default source
  • Loading branch information
Valeriy Nayda authored Jun 29, 2018
2 parents d8674bd + 8a93661 commit a840950
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
*/
class SourceDataProvider extends DataProvider
{
const SOURCE_FORM_NAME = 'inventory_source_form_data_source';

/**
* @var SourceRepositoryInterface
*/
Expand Down Expand Up @@ -88,25 +90,23 @@ public function __construct(
public function getData()
{
$data = parent::getData();
if ('inventory_source_form_data_source' === $this->name) {
if (self::SOURCE_FORM_NAME === $this->name) {
// It is need for support of several fieldsets.
// For details see \Magento\Ui\Component\Form::getDataSourceData
if ($data['totalRecords'] > 0) {
$sourceCode = $data['items'][0][SourceInterface::SOURCE_CODE];
$sourceGeneralData = $data['items'][0];
$sourceGeneralData['disable_source_code'] = !empty($sourceGeneralData['source_code']);
$dataForSingle[$sourceCode] = [
'general' => $sourceGeneralData,
];
$data = $dataForSingle;
} else {
$sessionData = $this->session->getSourceFormData(true);
if (null !== $sessionData) {
// For details see \Magento\Ui\Component\Form::getDataSourceData
$data = [
'' => $sessionData,
];
}
return $dataForSingle;
}
$sessionData = $this->session->getSourceFormData(true);
if (null !== $sessionData) {
// For details see \Magento\Ui\Component\Form::getDataSourceData
$data = [
'' => $sessionData,
];
}
}
return $data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@
</validation>
<dataType>text</dataType>
<label translate="true">Code</label>
<imports>
<link name="disabled">${ $.provider }:data.general.disable_source_code</link>
</imports>
</settings>
</field>
<field name="name" formElement="input" sortOrder="20">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventoryCatalogAdminUi\Plugin\InventoryAdminUi\DataProvider;

use Magento\Framework\App\RequestInterface;
use Magento\InventoryApi\Api\Data\SourceItemInterface;
use Magento\InventoryCatalogApi\Api\DefaultSourceProviderInterface;
use Magento\InventoryAdminUi\Ui\DataProvider\SourceDataProvider;

class PreventDisablingDefaultSourcePlugin
{
/**
* @var DefaultSourceProviderInterface
*/
private $defaultSourceProvider;

/**
* @var RequestInterface
*/
private $request;

/**
* @param DefaultSourceProviderInterface $defaultSourceProvider
* @param RequestInterface $request
*/
public function __construct(
DefaultSourceProviderInterface $defaultSourceProvider,
RequestInterface $request
) {
$this->defaultSourceProvider = $defaultSourceProvider;
$this->request = $request;
}

/**
* @param SourceDataProvider $subject
* @param $meta
* @return array
*/
public function afterGetMeta(
SourceDataProvider $subject,
$meta
): array {
$isFormComponent = SourceDataProvider::SOURCE_FORM_NAME === $subject->getName();
if (!$isFormComponent || !$this->isDefaultSource()) {
return $meta;
}

$meta['general'] = [
'children' => [
'enabled' => [
'arguments' => [
'data' => [
'config' => [
'disabled' => true,
]
]
]
]
]
];

return $meta;
}

/**
* @return bool
*/
private function isDefaultSource(): bool
{
$defaultSourceCode = $this->defaultSourceProvider->getCode();
$currentSourceCode = $this->request->getParam(SourceItemInterface::SOURCE_CODE);
return $defaultSourceCode === $currentSourceCode;
}
}
3 changes: 3 additions & 0 deletions app/code/Magento/InventoryCatalogAdminUi/etc/adminhtml/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,7 @@
<type name="Magento\InventoryAdminUi\Model\Stock\StockSourceLinkProcessor">
<plugin name="prevent_process_for_default_stock" type="Magento\InventoryCatalogAdminUi\Plugin\InventoryAdminUi\Stock\StockSaveProcessor\PreventProcessDefaultStockLinksPlugin"/>
</type>
<type name="Magento\InventoryAdminUi\Ui\DataProvider\SourceDataProvider">
<plugin name="prevent_disabling_default_source" type="Magento\InventoryCatalogAdminUi\Plugin\InventoryAdminUi\DataProvider\PreventDisablingDefaultSourcePlugin"/>
</type>
</config>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<fieldset name="general">
<field name="enabled">
<settings>
<notice translate="true">The Default Source must be enabled. A default source is required for single source merchants and product migration.</notice>
</settings>
</field>
</fieldset>
</form>

0 comments on commit a840950

Please sign in to comment.