Commit f6e336c4 authored by catch's avatar catch

Issue #2414953 by alexpott: Element uses \Drupal::service() as a service...

Issue #2414953 by alexpott: Element uses \Drupal::service() as a service locator and prevents injecting a custom typed config manager in config
parent 1e091e40
......@@ -7,6 +7,7 @@
namespace Drupal\Core\Config\Schema;
use Drupal\Core\Config\TypedConfigManagerInterface;
use Drupal\Core\TypedData\TypedData;
/**
......@@ -14,6 +15,13 @@
*/
abstract class Element extends TypedData {
/**
* The typed config manager.
*
* @var \Drupal\Core\Config\TypedConfigManagerInterface
*/
protected $typedConfig;
/**
* The configuration value.
*
......@@ -25,7 +33,7 @@ abstract class Element extends TypedData {
* Create typed config object.
*/
protected function parseElement($key, $data, $definition) {
return \Drupal::service('config.typed')->create($definition, $data, $key, $this);
return $this->typedConfig->create($definition, $data, $key, $this);
}
/**
......@@ -34,7 +42,19 @@ protected function parseElement($key, $data, $definition) {
* @return \Drupal\Core\TypedData\DataDefinitionInterface
*/
protected function buildDataDefinition($definition, $value, $key) {
return \Drupal::service('config.typed')->buildDataDefinition($definition, $value, $key, $this);
return $this->typedConfig->buildDataDefinition($definition, $value, $key, $this);
}
/**
* Sets the typed config manager on the instance.
*
* This must be called immediately after construction to enable
* self::parseElement() and self::buildDataDefinition() to work.
*
* @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config
*/
public function setTypedConfig(TypedConfigManagerInterface $typed_config) {
$this->typedConfig = $typed_config;
}
}
......@@ -10,6 +10,7 @@
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Config\Schema\ConfigSchemaDiscovery;
use Drupal\Core\Config\Schema\Element;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\TypedData\TypedDataManager;
......@@ -295,4 +296,17 @@ public function hasConfigSchema($name) {
return is_array($definition) && ($definition['class'] != '\Drupal\Core\Config\Schema\Undefined');
}
/**
* {@inheritdoc}
*/
public function createInstance($data_type, array $configuration = array()) {
$instance = parent::createInstance($data_type, $configuration);
// Enable elements to construct their own definitions using the typed config
// manager.
if ($instance instanceof Element) {
$instance->setTypedConfig($this);
}
return $instance;
}
}
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