From ba5b309131f20fd59b8f4c00f8df7e36fd6aac9e Mon Sep 17 00:00:00 2001 From: catch Date: Wed, 23 Nov 2022 10:18:00 +0000 Subject: [PATCH] Issue #2925297 by Spokje, mpp, paulvandenburg, anmolgoyal74, gobinathm, catch, mmrares, shubhangi1995, Martijn de Wit: Fatal error on config form with translation enabled when config is missing (cherry picked from commit d0ee369a05962eed22afcd49c765f1c56b77ead0) --- core/lib/Drupal/Core/Config/TypedConfigManager.php | 8 ++++++++ .../Drupal/KernelTests/Config/TypedConfigTest.php | 11 +++++++++++ 2 files changed, 19 insertions(+) diff --git a/core/lib/Drupal/Core/Config/TypedConfigManager.php b/core/lib/Drupal/Core/Config/TypedConfigManager.php index 8585f8399e7..06b81809258 100644 --- a/core/lib/Drupal/Core/Config/TypedConfigManager.php +++ b/core/lib/Drupal/Core/Config/TypedConfigManager.php @@ -2,6 +2,7 @@ namespace Drupal\Core\Config; +use Drupal\Component\Render\FormattableMarkup; use Drupal\Component\Utility\NestedArray; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Config\Schema\ConfigSchemaAlterException; @@ -75,6 +76,13 @@ class TypedConfigManager extends TypedDataManager implements TypedConfigManagerI */ public function get($name) { $data = $this->configStorage->read($name); + if ($data === FALSE) { + // For a typed config the data MUST exist. + $data = []; + trigger_error(new FormattableMarkup('Missing required data for typed configuration: @config', [ + '@config' => $name, + ]), E_USER_ERROR); + } return $this->createFromNameAndData($name, $data); } diff --git a/core/tests/Drupal/KernelTests/Config/TypedConfigTest.php b/core/tests/Drupal/KernelTests/Config/TypedConfigTest.php index 7a40310b316..adba8b8e0b3 100644 --- a/core/tests/Drupal/KernelTests/Config/TypedConfigTest.php +++ b/core/tests/Drupal/KernelTests/Config/TypedConfigTest.php @@ -9,6 +9,7 @@ use Drupal\Core\TypedData\ComplexDataInterface; use Drupal\Core\TypedData\Type\IntegerInterface; use Drupal\Core\TypedData\Type\StringInterface; use Drupal\KernelTests\KernelTestBase; +use PHPUnit\Framework\Error\Error; use Symfony\Component\Validator\ConstraintViolationListInterface; /** @@ -38,6 +39,16 @@ class TypedConfigTest extends KernelTestBase { public function testTypedDataAPI() { /** @var \Drupal\Core\Config\TypedConfigManagerInterface $typed_config_manager */ $typed_config_manager = \Drupal::service('config.typed'); + + // Test non-existent data. + try { + $typed_config_manager->get('config_test.non_existent'); + $this->fail('Expected error when trying to get non-existent typed config.'); + } + catch (Error $e) { + $this->assertEquals('Missing required data for typed configuration: config_test.non_existent', $e->getMessage()); + } + /** @var \Drupal\Core\Config\Schema\TypedConfigInterface $typed_config */ $typed_config = $typed_config_manager->get('config_test.validation');