Unverified Commit 733dc5fc authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2625212 by faline, bbrala, cilefen, bircher, okay19, krishnan.n, snehi,...

Issue #2625212 by faline, bbrala, cilefen, bircher, okay19, krishnan.n, snehi, tstoeckler, kostyashupenko, chi, alexpott, dawehner, wim leers, smustgrave, godotislate, cs_shadow, catch, berdir, mxr576: Add ConfigSchemaChecker to development.services.yml
parent 0f37fcb7
Loading
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -17,3 +17,14 @@ parameters:
services:
  cache.backend.null:
    class: Drupal\Core\Cache\NullBackendFactory
  logger.channel.config_schema:
    parent: logger.channel_base
    arguments: [ 'config_schema' ]
  config.schema_checker:
    class: Drupal\Core\Config\Development\LenientConfigSchemaChecker
    arguments:
      - '@config.typed'
      - '@messenger'
      - '@logger.channel.config_schema'
    tags:
      - { name: event_subscriber }
+50 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Core\Config\Development;

use Drupal\Core\Config\ConfigCrudEvent;
use Drupal\Core\Config\Schema\SchemaIncompleteException;
use Drupal\Core\Config\TypedConfigManagerInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Psr\Log\LoggerInterface;

/**
 * Listens to the config save event and warns about invalid schema.
 */
class LenientConfigSchemaChecker extends ConfigSchemaChecker {

  /**
   * Constructs the ConfigSchemaChecker object.
   *
   * @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_manager
   *   The typed config manager.
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   The messenger service to display the warning.
   * @param \Psr\Log\LoggerInterface $logger
   *   The logger to save the warning.
   * @param string[] $exclude
   *   An array of config object names that are excluded from schema checking.
   */
  public function __construct(TypedConfigManagerInterface $typed_manager, protected readonly MessengerInterface $messenger, protected readonly LoggerInterface $logger, array $exclude = []) {
    parent::__construct($typed_manager, $exclude);
  }

  /**
   * Checks that configuration complies with its schema on config save.
   *
   * @param \Drupal\Core\Config\ConfigCrudEvent $event
   *   The configuration event.
   */
  public function onConfigSave(ConfigCrudEvent $event): void {
    try {
      parent::onConfigSave($event);
    }
    catch (SchemaIncompleteException $exception) {
      $message = sprintf('%s. These errors mean there is configuration that does not comply with its schema. This is not a fatal error, but it is recommended to fix these issues. For more information on configuration schemas, check out <a href="%s">the documentation</a>.', $exception->getMessage(), 'https://www.drupal.org/docs/drupal-apis/configuration-api/configuration-schemametadata');

      $this->messenger->addWarning($message);
      $this->logger->warning($message);
    }
  }

}
+11 −0
Original line number Diff line number Diff line
@@ -17,3 +17,14 @@ parameters:
services:
  cache.backend.null:
    class: Drupal\Core\Cache\NullBackendFactory
  logger.channel.config_schema:
    parent: logger.channel_base
    arguments: [ 'config_schema' ]
  config.schema_checker:
    class: Drupal\Core\Config\Development\LenientConfigSchemaChecker
    arguments:
      - '@config.typed'
      - '@messenger'
      - '@logger.channel.config_schema'
    tags:
      - { name: event_subscriber }