Skip to content

Commit a4a5c9a

Browse files
author
Bohdan Korablov
committed
MAGETWO-61786: Implementation
1 parent 8a0cf91 commit a4a5c9a

File tree

14 files changed

+544
-12
lines changed

14 files changed

+544
-12
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,19 @@
141141
<type name="Magento\Backend\Model\Menu\Builder">
142142
<plugin name="SetupMenuBuilder" type="Magento\Backend\Model\Setup\MenuBuilder" />
143143
</type>
144+
<type name="Magento\Config\Model\Config\Structure\ProductionVisibility">
145+
<arguments>
146+
<argument name="configs" xsi:type="array">
147+
<item name="dev" xsi:type="const">Magento\Config\Model\Config\Structure\ElementVisibilityInterface::HIDDEN</item>
148+
<item name="general/locale/code" xsi:type="const">Magento\Config\Model\Config\Structure\ElementVisibilityInterface::DISABLED</item>
149+
</argument>
150+
</arguments>
151+
</type>
152+
<type name="Magento\Config\Model\Config\Structure\ElementVisibility">
153+
<arguments>
154+
<argument name="visibility" xsi:type="array">
155+
<item name="productionVisibility" xsi:type="object">Magento\Config\Model\Config\Structure\ProductionVisibility</item>
156+
</argument>
157+
</arguments>
158+
</type>
144159
</config>

app/code/Magento/Config/Block/System/Config/Form.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Framework\App\DeploymentConfig;
1212
use Magento\Framework\App\ObjectManager;
1313
use Magento\Framework\DataObject;
14+
use Magento\Config\Model\Config\Structure\ElementVisibilityInterface;
1415

1516
/**
1617
* System config form block
@@ -113,6 +114,11 @@ class Form extends \Magento\Backend\Block\Widget\Form\Generic
113114
*/
114115
private $appConfig;
115116

117+
/**
118+
* @var ElementVisibilityInterface
119+
*/
120+
private $elementVisibility;
121+
116122
/**
117123
* @param \Magento\Backend\Block\Template\Context $context
118124
* @param \Magento\Framework\Registry $registry
@@ -355,7 +361,10 @@ protected function _initElement(
355361
$sharedClass = $this->_getSharedCssClass($field);
356362
$requiresClass = $this->_getRequiresCssClass($field, $fieldPrefix);
357363

358-
$isReadOnly = $this->getSettingChecker()->isReadOnly($path, $this->getScope(), $this->getStringScopeCode());
364+
$path = trim($field->getPath(), '/');
365+
$isReadOnly = $this->getElementVisibility()->isDisabled($path);
366+
$isReadOnly = $isReadOnly
367+
?: $this->getSettingChecker()->isReadOnly($path, $this->getScope(), $this->getStringScopeCode());
359368
$formField = $fieldset->addField(
360369
$elementId,
361370
$field->getType(),
@@ -805,4 +814,19 @@ private function getAppConfigDataValue($path)
805814
}
806815
return $data->getData($path);
807816
}
817+
818+
/**
819+
* Get instance of ElementVisibilityInterface.
820+
*
821+
* @return ElementVisibilityInterface
822+
* @deprecated
823+
*/
824+
public function getElementVisibility()
825+
{
826+
if (null === $this->elementVisibility) {
827+
$this->elementVisibility = ObjectManager::getInstance()->get(ElementVisibilityInterface::class);
828+
}
829+
830+
return $this->elementVisibility;
831+
}
808832
}

app/code/Magento/Config/Model/Config/Structure/AbstractElement.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use Magento\Framework\App\Config\ScopeConfigInterface;
99
use Magento\Store\Model\StoreManagerInterface;
10+
use Magento\Framework\App\ObjectManager;
1011

1112
abstract class AbstractElement implements ElementInterface
1213
{
@@ -36,6 +37,11 @@ abstract class AbstractElement implements ElementInterface
3637
*/
3738
protected $moduleManager;
3839

40+
/**
41+
* @var ElementVisibilityInterface
42+
*/
43+
private $elementVisibility;
44+
3945
/**
4046
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
4147
* @param \Magento\Framework\Module\Manager $moduleManager
@@ -141,6 +147,11 @@ public function getAttribute($key)
141147
*/
142148
public function isVisible()
143149
{
150+
$path = trim($this->getPath(), '/');
151+
if ($this->getElementVisibility()->isHidden($path)) {
152+
return false;
153+
}
154+
144155
if (isset($this->_data['if_module_enabled']) &&
145156
!$this->moduleManager->isOutputEnabled($this->_data['if_module_enabled'])) {
146157
return false;
@@ -203,4 +214,19 @@ public function getPath($fieldPrefix = '')
203214
{
204215
return $this->_getPath($this->getId(), $fieldPrefix);
205216
}
217+
218+
/**
219+
* Get instance of ElementVisibilityInterface.
220+
*
221+
* @return ElementVisibilityInterface
222+
* @deprecated
223+
*/
224+
public function getElementVisibility()
225+
{
226+
if (null === $this->elementVisibility) {
227+
$this->elementVisibility = ObjectManager::getInstance()->get(ElementVisibilityInterface::class);
228+
}
229+
230+
return $this->elementVisibility;
231+
}
206232
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Config\Model\Config\Structure;
7+
8+
use Magento\Framework\Exception\ConfigurationMismatchException;
9+
10+
/**
11+
* Contains list of classes which implement ElementVisibilityInterface for checking of visibility of form elements.
12+
*/
13+
class ElementVisibility implements ElementVisibilityInterface
14+
{
15+
/**
16+
* @var ElementVisibilityInterface[]
17+
*/
18+
private $visibility = [];
19+
20+
/**
21+
* @param ElementVisibilityInterface[] $visibility
22+
* @throws ConfigurationMismatchException
23+
*/
24+
public function __construct(array $visibility = [])
25+
{
26+
foreach ($visibility as $name => $item) {
27+
if (!$item instanceof ElementVisibilityInterface) {
28+
throw new ConfigurationMismatchException(
29+
__('%1 is not instance on %2', $name, ElementVisibilityInterface::class)
30+
);
31+
}
32+
}
33+
34+
$this->visibility = $visibility;
35+
}
36+
37+
/**
38+
* {@inheritdoc}
39+
*/
40+
public function isHidden($path)
41+
{
42+
foreach ($this->visibility as $element) {
43+
if ($element->isHidden($path)) {
44+
return true;
45+
}
46+
}
47+
48+
return false;
49+
}
50+
51+
/**
52+
* {@inheritdoc}
53+
*/
54+
public function isDisabled($path)
55+
{
56+
foreach ($this->visibility as $element) {
57+
if ($element->isDisabled($path)) {
58+
return true;
59+
}
60+
}
61+
62+
return false;
63+
}
64+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Config\Model\Config\Structure;
7+
8+
/**
9+
* Checks visibility of form elements by their paths in the structure.
10+
*/
11+
interface ElementVisibilityInterface
12+
{
13+
/**#@+
14+
* Constants of statuses for form elements.
15+
*/
16+
const HIDDEN = 'hidden';
17+
const DISABLED = 'disabled';
18+
/**#@-*/
19+
20+
/**
21+
* Check whether form element is disabled by path.
22+
*
23+
* @param string $path
24+
* @return bool
25+
*/
26+
public function isDisabled($path);
27+
28+
/**
29+
* Check whether form element is hidden from form by path.
30+
*
31+
* @param string $path
32+
* @return bool
33+
*/
34+
public function isHidden($path);
35+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Config\Model\Config\Structure;
7+
8+
use Magento\Framework\App\State;
9+
10+
/**
11+
* Contains list of elements paths which should be hidden or disabled on Configuration form in Production mode.
12+
*/
13+
class ProductionVisibility implements ElementVisibilityInterface
14+
{
15+
/**
16+
* @var array
17+
*/
18+
private $configs = [];
19+
20+
/**
21+
* @var State
22+
*/
23+
private $state;
24+
25+
/**
26+
* @param State $state
27+
* @param array $configs
28+
*/
29+
public function __construct(State $state, array $configs = [])
30+
{
31+
$this->state = $state;
32+
$this->configs = $configs;
33+
}
34+
35+
/**
36+
* {@inheritdoc}
37+
*/
38+
public function isHidden($path)
39+
{
40+
return $this->state->getMode() === State::MODE_PRODUCTION
41+
&& !empty($this->configs[$path])
42+
&& $this->configs[$path] === static::HIDDEN;
43+
}
44+
45+
/**
46+
* {@inheritdoc}
47+
*/
48+
public function isDisabled($path)
49+
{
50+
return $this->state->getMode() === State::MODE_PRODUCTION
51+
&& !empty($this->configs[$path])
52+
&& $this->configs[$path] === static::DISABLED;
53+
}
54+
}

0 commit comments

Comments
 (0)