diff --git a/core/lib/Drupal/Core/Config/Schema/Element.php b/core/lib/Drupal/Core/Config/Schema/Element.php index c492d42..4993de8 100644 --- a/core/lib/Drupal/Core/Config/Schema/Element.php +++ b/core/lib/Drupal/Core/Config/Schema/Element.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Config\Schema; +use Drupal\Core\Config\TypedConfigManagerInterface; use Drupal\Core\TypedData\TypedData; /** @@ -15,6 +16,13 @@ abstract class Element extends TypedData { /** + * The typed config manager. + * + * @var \Drupal\Core\Config\TypedConfigManagerInterface + */ + protected $typedConfig; + + /** * The configuration value. * * @var mixed @@ -25,7 +33,7 @@ * 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; } } diff --git a/core/lib/Drupal/Core/Config/TypedConfigManager.php b/core/lib/Drupal/Core/Config/TypedConfigManager.php index fb1c198..a784364 100644 --- a/core/lib/Drupal/Core/Config/TypedConfigManager.php +++ b/core/lib/Drupal/Core/Config/TypedConfigManager.php @@ -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; + } + }