3

I would like to update my module schema variable from string to integer.

mymodule.settings:
  type: config_object
  label: 'Module settings'
  mapping:
    identity:
      type: string -> integer
      label: 'Identity'

I've changed it in the schema.yml file and also I've created a hook update to update it:

function mymodule_update_8001() {
  $facility = (int) \Drupal::configFactory()->getEditable('mymodule.settings')->get('identity');
  \Drupal::configFactory()->getEditable('syslog.settings')->set('identity', $facility)->save();
}

But after the update the variable is still a string. I've been digging out and the problem is that the configuration schema cast the value as string because the cache of typed_config_definitions has the old definition.

Drupal\Core\Config;

  public function save($has_trusted_data = FALSE) {
    // Validate the configuration object name before saving.
    static::validateName($this->name);

    // If there is a schema for this configuration object, cast all values to
    // conform to the schema.
    if (!$has_trusted_data) {
      if ($this->typedConfigManager->hasConfigSchema($this->name)) {
        // Ensure that the schema wrapper has the latest data.
        $this->schemaWrapper = NULL;
        foreach ($this->data as $key => $value) {
          $this->data[$key] = $this->castValue($key, $value);
        }
      }
      else {
        foreach ($this->data as $key => $value) {
          $this->validateValue($key, $value);
        }
      }
    }
...
}

So:

  • Should I force the update with $has_trusted_data = TRUE ?
  • Should I reset the CID typed_config_definitions cache in the hook update? how?
  • Is there any easiest way to do all this?

Thanks!

  • The config.typed service provides config schema and is actually a plugin manager. Like all plugin managers, there is a clearCachedDefinitions() method. Try calling that and then save. – Berdir Feb 23 '17 at 18:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.