diff --git a/core/lib/Drupal/Core/Config/Schema/Element.php b/core/lib/Drupal/Core/Config/Schema/Element.php index c492d42d73b31146e5626073d36cef536b290910..4993de8e3c132a7cacbe82060d57e3cf59b3dba2 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; /** @@ -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; } } diff --git a/core/lib/Drupal/Core/Config/TypedConfigManager.php b/core/lib/Drupal/Core/Config/TypedConfigManager.php index fb1c19808eb6d7aa4786ab72aa31b0ca114e78ec..a78436404a061009dcb56c384af2960c4936f90f 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; + } + }