0

We have a custom module with the following files. It is mainly used to store a list of content types that this configuration applies to. When saving the configuration, we get the following code.

How do we properly set the mymodule.schema.yml file to save the checkboxes?

Error Message. We also get the error message if we select Basic Page from the configuration.

The website encountered an unexpected error. Please try again later.InvalidArgumentException: The configuration property mymodule_enabled_content_types.article doesn't exist. in Drupal\Core\Config\Schema\ArrayElement->get() (line 76 of core\lib\Drupal\Core\Config\Schema\ArrayElement.php). Drupal\Core\Config\StorableConfigBase->castValue('mymodule_enabled_content_types.article ', 'article ') (Line: 211)

mymodule/config/schema/mymodule.schema.yml

mymodule.settings:
  type: config_object
  label: 'My Module settings'
  mapping: 
    mymodule_enabled_content_types:
      type: string
      label: 'Enabled Content types'

mymodule/config/install/mymodule.settings.yml

mymodule_enabled_content_types: ''

mymoduleForm.php

...
public function buildForm(array $form, FormStateInterface $form_state) {
  $config->$this->config('mymodule.settings');
  // Get all Content Types.
  $types = \Drupal::entityTypeManager()
    ->getStorage('node_type')
    ->loadMultiple();

  foreach($types as $type) {
    $content_types[$type->id()] = $type->label();
  }
  $form['mymodule_enabled_content_types'] = [
    '#type' => 'checkboxes',
    '#title' => $this->t('Enabled Content Types'),
    '#description' => $this->t('Define what content types will be enabled.'),
    '#default_value' => $config->get('mymodule_custom_variable'),
    '#options' => $content_types,
  ];
}

public function submitForm(array &$form, FormStateInterface $form_state) {
    // Retrieve the configuration
    $this->configFactory->getEditable('mymodule.settings')
      ->set('mymodule_enabled_content_types', $form_state->getValue('mymodule_enabled_content_types'))
      ->save();

    parent::submitForm($form, $form_state);
  }
}
...
2

You are creating your checkbox options as an associative array of node type (bundle) IDs as keys and their labels as values.

When this form is submitted, checked checkboxes submit as key => key. So the stored information (I suppose 'rss_url' in your question is intended to being 'mymodule_enabled_content_types') is similar to:

mymodule_enabled_content_types:
  article: article
  page: page
  custom: custom

As the keys of the submitted values are unknown before submission, and the values clearly are strings, this is a sequence of strings (compare https://www.drupal.org/docs/8/api/configuration-api/configuration-schemametadata#properties). The correct schema definition is:

mymodule.settings:
  type: config_object
  label: 'My Module settings'
  mapping: 
    mymodule_enabled_content_types:
      type: sequence
      label: 'Enabled Content types'
      sequence:
        type: string

After changing your schema definition and rebuilding your caches, your form submit handler should work just fine and the configuration being saved using its correct data types.

  • yes that was a typo that i fixed, good catch. When i used the sequence as you mentioned it worked. – usernameabc Aug 9 '18 at 16:00

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.