0

We have a custom module, mymodule that has an admin form where it stores field of type: number and select. I need to create a mymodule/config/schema/mymodule.schema.yml file, but don't know what to put in there.

What is the correct way to define the mymodule.schema.yml file for the two field types?

mymoduleForm.php

class MyModuleForm extends ConfigFormBase {
  protected function getEditableConfigNames() {
    return [
      'mymodule.setting',
    ];
  }
  public function getFormId() {
    return 'mymodule';
  }
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this->config('mymodule.setting');
    $form['field_select'] = [
      '#type' => 'select',
      '#title' => 'Selection',
      '#description' => 'Pick one.',
      '#options' => [
        '1' => $this->t('option 1'),
        '2' => $this->t('option 2'),
      ],
      '#default_value' => $config->get('field_select'),
    ];

    $form['field_num'] = [
      '#type' => 'number',
      '#title' => $this->t('Some number'),
      '#description' => 'Indicate number.',
      '#default_value' => $config->get('field_num'),
    ];
  }
}

Here is what I was thinking but it does not look correct.

mymodule.schema.yml

mymodule.setting:
  type: config_object
  label: 'My form'
  mapping:
    field_num:
      type: integer
      label: 'My number'
    field_select:
      type: string
      label: 'My selection'

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.