Commit a9fe99c7 authored by alexpott's avatar alexpott

Issue #2869809 by dawehner, effulgentsia, tim.plunkett, tstoeckler: Make it...

Issue #2869809 by dawehner, effulgentsia, tim.plunkett, tstoeckler: Make it easy to get typed config representations of entities
parent d284ae82
......@@ -55,9 +55,7 @@ public function checkConfigSchema(TypedConfigManagerInterface $typed_config, $co
if (!$typed_config->hasConfigSchema($config_name)) {
return FALSE;
}
$definition = $typed_config->getDefinition($config_name);
$data_definition = $typed_config->buildDataDefinition($definition, $config_data);
$this->schema = $typed_config->create($data_definition, $config_data);
$this->schema = $typed_config->createFromNameAndData($config_name, $config_data);
$errors = [];
foreach ($config_data as $key => $value) {
$errors = array_merge($errors, $this->checkValue($key, $value));
......
......@@ -131,9 +131,7 @@ public function getStorage() {
*/
protected function getSchemaWrapper() {
if (!isset($this->schemaWrapper)) {
$definition = $this->typedConfigManager->getDefinition($this->name);
$data_definition = $this->typedConfigManager->buildDataDefinition($definition, $this->data);
$this->schemaWrapper = $this->typedConfigManager->create($data_definition, $this->data);
$this->schemaWrapper = $this->typedConfigManager->createFromNameAndData($this->name, $this->data);
}
return $this->schemaWrapper;
}
......
......@@ -75,9 +75,7 @@ protected function getDiscovery() {
*/
public function get($name) {
$data = $this->configStorage->read($name);
$type_definition = $this->getDefinition($name);
$data_definition = $this->buildDataDefinition($type_definition, $data);
return $this->create($data_definition, $data);
return $this->createFromNameAndData($name, $data);
}
/**
......@@ -387,4 +385,13 @@ protected function alterDefinitions(&$definitions) {
}
}
/**
* {@inheritdoc}
*/
public function createFromNameAndData($config_name, array $config_data) {
$definition = $this->getDefinition($config_name);
$data_definition = $this->buildDataDefinition($definition, $config_data);
return $this->create($data_definition, $config_data);
}
}
......@@ -72,4 +72,18 @@ public function hasConfigSchema($name);
*/
public function getDefinition($plugin_id, $exception_on_invalid = TRUE);
/**
* Gets typed data for a given configuration name and its values.
*
* @param string $config_name
* The machine name of the configuration.
* @param array $config_data
* The data associated with the configuration. Note: This configuration
* doesn't yet have to be stored.
*
* @return \Drupal\Core\TypedData\TraversableTypedDataInterface
* The typed configuration element.
*/
public function createFromNameAndData($config_name, array $config_data);
}
......@@ -140,9 +140,7 @@ public function getTranslatableDefaultConfig($name) {
if ($this->isSupported($name)) {
// Create typed configuration wrapper based on install storage data.
$data = $this->defaultConfigStorage->read($name);
$type_definition = $this->typedConfigManager->getDefinition($name);
$data_definition = $this->typedConfigManager->buildDataDefinition($type_definition, $data);
$typed_config = $this->typedConfigManager->create($data_definition, $data);
$typed_config = $this->typedConfigManager->createFromNameAndData($name, $data);
if ($typed_config instanceof TraversableTypedDataInterface) {
return $this->getTranslatableData($typed_config);
}
......
......@@ -3,6 +3,7 @@
namespace Drupal\KernelTests\Config;
use Drupal\Core\Config\Schema\SequenceDataDefinition;
use Drupal\Core\Config\Schema\TypedConfigInterface;
use Drupal\Core\TypedData\ComplexDataDefinitionInterface;
use Drupal\Core\TypedData\ComplexDataInterface;
use Drupal\Core\TypedData\Type\IntegerInterface;
......@@ -68,6 +69,24 @@ public function testTypedDataAPI() {
$this->assertEquals(2, count($sequence->getIterator()));
// Verify the item metadata is available.
$this->assertInstanceOf(SequenceDataDefinition::class, $sequence->getDataDefinition());
// Test accessing typed config objects for simple config and config
// entities.
$typed_config_manager = \Drupal::service('config.typed');
$typed_config = $typed_config_manager->createFromNameAndData('config_test.validation', \Drupal::configFactory()->get('config_test.validation')->get());
$this->assertInstanceOf(TypedConfigInterface::class, $typed_config);
$this->assertEquals(['llama', 'cat', 'giraffe', 'uuid', '_core'], array_keys($typed_config->getElements()));
$config_test_entity = \Drupal::entityTypeManager()->getStorage('config_test')->create([
'id' => 'asterix',
'label' => 'Asterix',
'weight' => 11,
'style' => 'test_style',
]);
$typed_config = $typed_config_manager->createFromNameAndData($config_test_entity->getConfigDependencyName(), $config_test_entity->toArray());
$this->assertInstanceOf(TypedConfigInterface::class, $typed_config);
$this->assertEquals(['uuid', 'langcode', 'status', 'dependencies', 'id', 'label', 'weight', 'style', 'size', 'size_value', 'protected_property'], array_keys($typed_config->getElements()));
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment