Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1250: added plugin to prevent disabling default source #1410

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>