Issue #2317865 by alexpott, mdrummond: Fixed Config schema definitions for plugins aren't applied to their derivatives.

8.0.x
Dries 2014-08-13 16:06:29 -04:00
parent 8aa8c492c4
commit 8ed2e2058e
4 changed files with 144 additions and 11 deletions

View File

@ -81,7 +81,8 @@ class TypedConfigManager extends TypedDataManager implements TypedConfigManagerI
// Add default values for data type and replace variables.
$definition += array('type' => 'undefined');
if (strpos($definition['type'], ']')) {
$type = $definition['type'];
if (strpos($type, ']')) {
// Replace variable names in definition.
$replace = is_array($value) ? $value : array();
if (isset($parent)) {
@ -90,10 +91,13 @@ class TypedConfigManager extends TypedDataManager implements TypedConfigManagerI
if (isset($name)) {
$replace['%key'] = $name;
}
$definition['type'] = $this->replaceName($definition['type'], $replace);
$type = $this->replaceName($type, $replace);
// Remove the type from the definition so that it is replaced with the
// concrete type from schema definitions.
unset($definition['type']);
}
// Add default values from type definition.
$definition += $this->getDefinition($definition['type']);
$definition += $this->getDefinition($type);
$data_definition = $this->createDataDefinition($definition['type']);
@ -147,15 +151,16 @@ class TypedConfigManager extends TypedDataManager implements TypedConfigManagerI
}
/**
* Gets fallback metadata name.
* Gets fallback configuration schema name.
*
* @param string $name
* Configuration name or key.
*
* @return null|string
* Same name with the last part(s) replaced by the filesystem marker.
* for example, breakpoint.breakpoint.module.toolbar.narrow check for
* definition in below order:
* The resolved schema name for the given configuration name or key. Returns
* null if there is no schema name to fallback to. For example,
* breakpoint.breakpoint.module.toolbar.narrow will check for definitions in
* the following order:
* breakpoint.breakpoint.module.toolbar.*
* breakpoint.breakpoint.module.*.*
* breakpoint.breakpoint.module.*
@ -163,12 +168,19 @@ class TypedConfigManager extends TypedDataManager implements TypedConfigManagerI
* breakpoint.breakpoint.*
* breakpoint.*.*.*.*
* breakpoint.*
* Returns null, if no matching element.
* Colons are also used, for example,
* block.settings.system_menu_block:footer will check for definitions in the
* following order:
* block.settings.system_menu_block:*
* block.settings.*:*
* block.settings.*
* block.*.*:*
* block.*
*/
protected function getFallbackName($name) {
// Check for definition of $name with filesystem marker.
$replaced = preg_replace('/(\.[^\.]+)([\.\*]*)$/', '.*\2', $name);
if ($replaced != $name ) {
$replaced = preg_replace('/([^\.:]+)([\.:\*]*)$/', '*\2', $name);
if ($replaced != $name) {
if (isset($this->definitions[$replaced])) {
return $replaced;
}
@ -177,7 +189,7 @@ class TypedConfigManager extends TypedDataManager implements TypedConfigManagerI
// wildcard to see if there is a greedy match. For example,
// breakpoint.breakpoint.*.* becomes
// breakpoint.breakpoint.*
$one_star = preg_replace('/\.([\.\*]*)$/', '.*', $replaced);
$one_star = preg_replace('/\.([:\.\*]*)$/', '.*', $replaced);
if ($one_star != $replaced && isset($this->definitions[$one_star])) {
return $one_star;
}

View File

@ -397,4 +397,37 @@ class ConfigSchemaTest extends DrupalUnitTestBase {
$this->assertIdentical($definition, $definition2);
}
/**
* Tests use of colons in schema type determination.
*
* @see \Drupal\Core\Config\TypedConfigManager::getFallbackName()
*/
function testColonsInSchemaTypeDetermination() {
$tests = \Drupal::service('config.typed')->get('config_schema_test.plugin_types')->get('tests');
$definition = $tests[0]->getDataDefinition()->toArray();
$this->assertEqual($definition['type'], 'test.plugin_types.boolean');
$definition = $tests[1]->getDataDefinition()->toArray();
$this->assertEqual($definition['type'], 'test.plugin_types.boolean:*');
$definition = $tests[2]->getDataDefinition()->toArray();
$this->assertEqual($definition['type'], 'test.plugin_types.*');
$definition = $tests[3]->getDataDefinition()->toArray();
$this->assertEqual($definition['type'], 'test.plugin_types.*');
$tests = \Drupal::service('config.typed')->get('config_schema_test.plugin_types')->get('test_with_parents');
$definition = $tests[0]['settings']->getDataDefinition()->toArray();
$this->assertEqual($definition['type'], 'test_with_parents.plugin_types.boolean');
$definition = $tests[1]['settings']->getDataDefinition()->toArray();
$this->assertEqual($definition['type'], 'test_with_parents.plugin_types.boolean:*');
$definition = $tests[2]['settings']->getDataDefinition()->toArray();
$this->assertEqual($definition['type'], 'test_with_parents.plugin_types.*');
$definition = $tests[3]['settings']->getDataDefinition()->toArray();
$this->assertEqual($definition['type'], 'test_with_parents.plugin_types.*');
}
}

View File

@ -0,0 +1,30 @@
tests:
-
plugin_id: boolean
value: TRUE
-
plugin_id: boolean:derivative
value: TRUE
-
plugin_id: string
value: 'Foo'
-
plugin_id: string:derivative
value: 'Foo'
test_with_parents:
-
plugin_id: boolean
settings:
value: TRUE
-
plugin_id: boolean:derivative
settings:
value: TRUE
-
plugin_id: string
settings:
value: 'Foo'
-
plugin_id: string:derivative
settings:
value: 'Foo'

View File

@ -149,3 +149,61 @@ config_schema_test.ignore:
weight:
type: integer
label: 'Weight'
config_schema_test.plugin_types:
type: mapping
mapping:
tests:
type: sequence
sequence:
- type: test.plugin_types.[plugin_id]
test_with_parents:
type: sequence
sequence:
- type: mapping
mapping:
plugin_id:
type: string
settings:
type: test_with_parents.plugin_types.[%parent.plugin_id]
test.plugin_types:
type: mapping
mapping:
plugin_id:
type: string
test.plugin_types.boolean:
type: mapping
mapping:
plugin_id:
type: string
value:
type: boolean
test.plugin_types.boolean:*:
type: test.plugin_types.boolean
test_with_parents.plugin_types.boolean:
type: mapping
mapping:
value:
type: boolean
test_with_parents.plugin_types.boolean:*:
type: test_with_parents.plugin_types.boolean
test.plugin_types.*:
type: mapping
mapping:
plugin_id:
type: string
value:
type: string
test_with_parents.plugin_types.*:
type: mapping
mapping:
value:
type: string