function SchemaCheckTrait::checkConfigSchema

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Config/Schema/SchemaCheckTrait.php \Drupal\Core\Config\Schema\SchemaCheckTrait::checkConfigSchema()
  2. 8.9.x core/lib/Drupal/Core/Config/Schema/SchemaCheckTrait.php \Drupal\Core\Config\Schema\SchemaCheckTrait::checkConfigSchema()
  3. 11.x core/lib/Drupal/Core/Config/Schema/SchemaCheckTrait.php \Drupal\Core\Config\Schema\SchemaCheckTrait::checkConfigSchema()

Checks the TypedConfigManager has a valid schema for the configuration.

Parameters

\Drupal\Core\Config\TypedConfigManagerInterface $typed_config: The TypedConfigManager.

string $config_name: The configuration name.

array $config_data: The configuration data, assumed to be data for a top-level config object.

bool $validate_constraints: Determines if constraints will be validated. If TRUE, constraint validation errors will be added to the errors found.

Return value

array|bool FALSE if no schema found. List of errors if any found. TRUE if fully valid.

3 calls to SchemaCheckTrait::checkConfigSchema()
ConfigSchemaChecker::onConfigSave in core/lib/Drupal/Core/Config/Development/ConfigSchemaChecker.php
Checks that configuration complies with its schema on config save.
SchemaCheckTestTrait::assertConfigSchema in core/tests/Drupal/Tests/SchemaCheckTestTrait.php
Asserts the TypedConfigManager has a valid schema for the configuration.
SchemaCheckTraitTest::testCheckConfigSchema in core/tests/Drupal/KernelTests/Core/Config/SchemaCheckTraitTest.php
Tests \Drupal\Core\Config\Schema\SchemaCheckTrait.

File

core/lib/Drupal/Core/Config/Schema/SchemaCheckTrait.php, line 81

Class

SchemaCheckTrait
Provides a trait for checking configuration schema.

Namespace

Drupal\Core\Config\Schema

Code

public function checkConfigSchema(TypedConfigManagerInterface $typed_config, $config_name, $config_data, bool $validate_constraints = FALSE) {
    // We'd like to verify that the top-level type is either config_base,
    // config_entity, or a derivative. The only thing we can really test though
    // is that the schema supports having langcode in it. So add 'langcode' to
    // the data if it doesn't already exist.
    if (!isset($config_data['langcode'])) {
        $config_data['langcode'] = 'en';
    }
    $this->configName = $config_name;
    if (!$typed_config->hasConfigSchema($config_name)) {
        return FALSE;
    }
    $this->schema = $typed_config->createFromNameAndData($config_name, $config_data);
    $errors = [];
    foreach ($config_data as $key => $value) {
        $errors[] = $this->checkValue($key, $value);
    }
    $errors = array_merge(...$errors);
    if ($validate_constraints) {
        // Also perform explicit validation. Note this does NOT require every node
        // in the config schema tree to have validation constraints defined.
        $violations = $this->schema
            ->validate();
        $filtered_violations = array_filter(iterator_to_array($violations), fn(ConstraintViolation $v) => !static::isViolationForIgnoredPropertyPath($v));
        $validation_errors = array_map(fn(ConstraintViolation $v) => sprintf("[%s] %s", $v->getPropertyPath(), (string) $v->getMessage()), $filtered_violations);
        // @todo Decide in https://www.drupal.org/project/drupal/issues/3395099 when/how to trigger deprecation errors or even failures for contrib modules.
        $errors = array_merge($errors, $validation_errors);
    }
    if (empty($errors)) {
        return TRUE;
    }
    return $errors;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.