Skip to content

Allow configuration paths based on system.xml #31621

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

Merged
merged 3 commits into from
Jan 30, 2021
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
6 changes: 6 additions & 0 deletions app/code/Magento/Config/Model/Config/PathValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace Magento\Config\Model\Config;

use Magento\Config\Model\Config\Structure\Element\Field;
use Magento\Framework\Exception\ValidatorException;

/**
Expand Down Expand Up @@ -40,6 +41,11 @@ public function __construct(Structure $structure)
*/
public function validate($path)
{
$element = $this->structure->getElementByConfigPath($path);
if ($element instanceof Field && $element->getConfigPath()) {
$path = $element->getConfigPath();
}

$allPaths = $this->structure->getFieldPaths();

if (!array_key_exists($path, $allPaths)) {
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Config/Model/Config/Structure.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public function getElementByConfigPath($path)
{
$allPaths = $this->getFieldPaths();

if (isset($allPaths[$path])) {
if (isset($allPaths[$path]) && is_array($allPaths[$path])) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain why it wasn't needed earlier?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We didn't call it with empty $allPaths before and with the changes at the 'validate' method we can have an error at $path = array_shift($allPaths[$path]) because '$allPaths[$path]' could be 0 (int) instead of array.

$path = array_shift($allPaths[$path]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace Magento\Config\Console\Command;

use Magento\Config\Model\Config\Backend\Admin\Custom;
use Magento\Config\Model\Config\PathValidator;
use Magento\Config\Model\Config\Structure\Converter;
use Magento\Config\Model\Config\Structure\Data as StructureData;
use Magento\Directory\Model\Currency;
Expand Down Expand Up @@ -444,6 +445,15 @@ public function configSetValidDataProvider()
];
}

/**
* Test validate path when field has custom config_path
*/
public function testValidatePathWithCustomConfigPath(): void
{
$pathValidator = $this->objectManager->get(PathValidator::class);
$this->assertTrue($pathValidator->validate('general/group/subgroup/second_field'));
}

/**
* Set configuration and check this value from DB with success message this command should display
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
<field id="field" translate="label" type="text" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Label</label>
</field>
<field id="second_field" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Label Second</label>
<config_path>custom/config_path/second_field</config_path>
</field>
</group>
</group>
</section>
Expand Down