Issue #3401186 by Wim Leers, alexpott: Follow-up for #3382510: Throw \LogicException when >1 #config_target in the same form targets the same property path
parent
8aa03123ec
commit
b7540532dc
|
@ -136,6 +136,14 @@ abstract class ConfigFormBase extends FormBase {
|
|||
$target = ConfigTarget::fromString($target);
|
||||
}
|
||||
foreach ($target->propertyPaths as $property_path) {
|
||||
if (isset($map[$target->configName][$property_path])) {
|
||||
throw new \LogicException(sprintf('Two #config_targets both target "%s" in the "%s" config: `%s` and `%s`.',
|
||||
$property_path,
|
||||
$target->configName,
|
||||
'$form[\'' . implode("']['", $map[$target->configName][$property_path]) . '\']',
|
||||
'$form[\'' . implode("']['", $element['#array_parents']) . '\']',
|
||||
));
|
||||
}
|
||||
$map[$target->configName][$property_path] = $element['#array_parents'];
|
||||
}
|
||||
$form_state->set(static::CONFIG_KEY_TO_FORM_ELEMENT_MAP, $map);
|
||||
|
|
|
@ -3,9 +3,14 @@
|
|||
namespace Drupal\Tests\Core\Form;
|
||||
|
||||
use Drupal\Core\Config\Config;
|
||||
use Drupal\Core\Config\ConfigFactoryInterface;
|
||||
use Drupal\Core\Config\TypedConfigManagerInterface;
|
||||
use Drupal\Core\Form\ConfigFormBase;
|
||||
use Drupal\Core\Form\ConfigTarget;
|
||||
use Drupal\Core\Form\ToConfig;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\Form\FormState;
|
||||
use Drupal\Core\Form\RedundantEditableConfigNamesTrait;
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
use Prophecy\Argument;
|
||||
|
||||
|
@ -15,6 +20,45 @@ use Prophecy\Argument;
|
|||
*/
|
||||
class ConfigTargetTest extends UnitTestCase {
|
||||
|
||||
/**
|
||||
* @covers \Drupal\Core\Form\ConfigFormBase::storeConfigKeyToFormElementMap
|
||||
*/
|
||||
public function testDuplicateTargetsNotAllowed(): void {
|
||||
$form = [
|
||||
'test' => [
|
||||
'#type' => 'text',
|
||||
'#default_value' => 'A test',
|
||||
'#config_target' => new ConfigTarget('system.site', 'admin_compact_mode', 'intval', 'boolval'),
|
||||
'#name' => 'test',
|
||||
'#array_parents' => ['test'],
|
||||
],
|
||||
'duplicate' => [
|
||||
'#type' => 'text',
|
||||
'#config_target' => new ConfigTarget('system.site', 'admin_compact_mode', 'intval', 'boolval'),
|
||||
'#name' => 'duplicate',
|
||||
'#array_parents' => ['duplicate'],
|
||||
],
|
||||
];
|
||||
|
||||
$test_form = new class(
|
||||
$this->prophesize(ConfigFactoryInterface::class)->reveal(),
|
||||
$this->prophesize(TypedConfigManagerInterface::class)->reveal(),
|
||||
) extends ConfigFormBase {
|
||||
use RedundantEditableConfigNamesTrait;
|
||||
|
||||
public function getFormId() {
|
||||
return 'test';
|
||||
}
|
||||
|
||||
};
|
||||
$form_state = new FormState();
|
||||
$test_form->storeConfigKeyToFormElementMap($form['test'], $form_state);
|
||||
|
||||
$this->expectException(\LogicException::class);
|
||||
$this->expectExceptionMessage('Two #config_targets both target "admin_compact_mode" in the "system.site" config: `$form[\'test\']` and `$form[\'duplicate\']`.');
|
||||
$test_form->storeConfigKeyToFormElementMap($form['duplicate'], $form_state);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::fromForm
|
||||
* @covers ::fromString
|
||||
|
|
Loading…
Reference in New Issue