Using interval widget in custom form

Created on 15 February 2025, 5 days ago

Problem/Motivation

Steps to reproduce

Create a dummy module with a settings form which includes an interval form element.

    $form['emails']['expiry_warning_period'] = [
      '#type' => 'interval',
      '#title' => $this->t('Expiry Warning Period'),
      '#description' => $this->t('Set the time before expiration that the warning email is sent.'),
      '#default_value' => $config->get('expiry_warning_period') ?? ['interval' => 1, 'period' => 'day'],
    ];

In the submitForm function, notice that it is necessary to get the values using the keys 'interval' and 'period'.
This makes it impossible to have more than one interval form element in the form.

  public function submitForm(array &$form, FormStateInterface $form_state) {
    $config = $this->config('my_module.settings');
    // This doesn't work...
    // $config->set('expiry_warning_period', $form_state->getValue('expiry_warning_period'));
    // But this does...
    $config->set('expiry_warning_period', ['interval' => $form_state->getValue('interval'), 'period' => $form_state->getValue('period')]);
    $config->save();

    parent::submitForm($form, $form_state);
  }
πŸ› Bug report
Status

Active

Version

1.14

Component

Code

Created by

πŸ‡¬πŸ‡§United Kingdom griz

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @griz
  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

    Thanks, the fix should be as simple as adding #tree => TRUE on your element, but I would also accept an MR to make that the default

Production build 0.71.5 2024