function ConfigSchemaChecker::onConfigSave

Same name in other branches
  1. 9 core/lib/Drupal/Core/Config/Development/ConfigSchemaChecker.php \Drupal\Core\Config\Development\ConfigSchemaChecker::onConfigSave()
  2. 8.9.x core/lib/Drupal/Core/Config/Development/ConfigSchemaChecker.php \Drupal\Core\Config\Development\ConfigSchemaChecker::onConfigSave()
  3. 11.x core/lib/Drupal/Core/Config/Development/ConfigSchemaChecker.php \Drupal\Core\Config\Development\ConfigSchemaChecker::onConfigSave()

Checks that configuration complies with its schema on config save.

Parameters

\Drupal\Core\Config\ConfigCrudEvent $event: The configuration event.

Throws

\Drupal\Core\Config\Schema\SchemaIncompleteException Exception thrown when configuration does not match its schema.

File

core/lib/Drupal/Core/Config/Development/ConfigSchemaChecker.php, line 75

Class

ConfigSchemaChecker
Listens to the config save event and validates schema.

Namespace

Drupal\Core\Config\Development

Code

public function onConfigSave(ConfigCrudEvent $event) {
    // Only validate configuration if in the default collection. Other
    // collections may have incomplete configuration (for example language
    // overrides only). These are not valid in themselves.
    $saved_config = $event->getConfig();
    if ($saved_config->getStorage()
        ->getCollectionName() != StorageInterface::DEFAULT_COLLECTION) {
        return;
    }
    $name = $saved_config->getName();
    $data = $saved_config->get();
    $checksum = Crypt::hashBase64(serialize($data));
    if (!in_array($name, $this->exclude) && !isset($this->checked[$name . ':' . $checksum])) {
        $this->checked[$name . ':' . $checksum] = TRUE;
        $errors = $this->checkConfigSchema($this->typedManager, $name, $data, $this->validateConstraints);
        if ($errors === FALSE) {
            throw new SchemaIncompleteException("No schema for {$name}");
        }
        elseif (is_array($errors)) {
            $text_errors = [];
            foreach ($errors as $key => $error) {
                $text_errors[] = new FormattableMarkup('@key @error', [
                    '@key' => $key,
                    '@error' => $error,
                ]);
            }
            throw new SchemaIncompleteException("Schema errors for {$name} with the following errors: " . implode(', ', $text_errors));
        }
    }
}

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