checkConfigSchema example

$form_state->getCompleteFormState()->setErrorByName($form_item_name$violation->getMessage());
    }

    // Pass it on to ::submitConfigurationForm().     $form_state->get('editor')->setSettings($settings);

    // Provide the validated eventual pair in form state to     // ::getGeneratedAllowedHtmlValue(), to update filter_html's     // "allowed_html".     $form_state->set('ckeditor5_validated_pair', $eventual_editor_and_format);

    assert(TRUE === $this->checkConfigSchema(\Drupal::getContainer()->get('config.typed'), 'editor.editor.id_does_not_matter', $submitted_editor->toArray()), 'Schema errors: ' . print_r($this->checkConfigSchema(\Drupal::getContainer()->get('config.typed'), 'editor.editor.id_does_not_matter', $submitted_editor->toArray()), TRUE));
  }

  /** * Gets the submitted text format config entity from form state. * * Needed for validation. * * @param \Drupal\Core\Form\FormStateInterface $filter_format_form_state * The text format configuration form's form state. * * @return \Drupal\filter\FilterFormatInterface * A FilterFormat config entity representing the current filter form state. */
    // 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);
      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));
      }
    }
  }
/** * Asserts the TypedConfigManager has a valid schema for the configuration. * * @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config * The TypedConfigManager. * @param string $config_name * The configuration name. * @param array $config_data * The configuration data. */
  public function assertConfigSchema(TypedConfigManagerInterface $typed_config$config_name$config_data) {
    $check = $this->checkConfigSchema($typed_config$config_name$config_data);
    $message = '';
    if ($check === FALSE) {
      $message = 'Error: No schema exists.';
    }
    elseif ($check !== TRUE) {
      $this->assertIsArray($check, "The config schema check errors should be in the form of an array.");
      $message = "Errors:\n";
      foreach ($check as $key => $error) {
        $message .= "Schema key $key failed with: $error\n";
      }
    }
    
protected function setUp(): void {
    parent::setUp();
    $this->installConfig(['config_test', 'config_schema_test']);
    $this->typedConfig = \Drupal::service('config.typed');
  }

  /** * Tests \Drupal\Core\Config\Schema\SchemaCheckTrait. */
  public function testTrait() {
    // Test a non existing schema.     $ret = $this->checkConfigSchema($this->typedConfig, 'config_schema_test.noschema', $this->config('config_schema_test.noschema')->get());
    $this->assertFalse($ret);

    // Test an existing schema with valid data.     $config_data = $this->config('config_test.types')->get();
    $ret = $this->checkConfigSchema($this->typedConfig, 'config_test.types', $config_data);
    $this->assertTrue($ret);

    // Add a new key, a new array and overwrite boolean with array to test the     // error messages.     $config_data = ['new_key' => 'new_value', 'new_array' => []] + $config_data;
    $config_data['boolean'] = [];
    
Home | Imprint | This part of the site doesn't use cookies.