Multiple Value Fields in Forms

Multiple Souvenir objects - Eric Prouzet on Unsplash

Just found a really useful module to provide a multi-value form element that wraps any number of form elements.

Got a bit stuck using it in a settings form to get the #default_value. 30 minutes of heavy Googling:

public function buildForm(array $form, FormStateInterface $form_state)
    $config = $this->config('new_node_notification.settings');
    $recipients = $config->get('recipients');
    $form['recipients'] = [
      '#type' => 'multivalue',
      '#title' => $this->t('Recipient emails'),
      '#description' => $this->t('People who should be notified of a new meeting/agenda being posted on the website.'),
      'email' => [
        '#type' => 'textfield',
        '#title' => $this->t('Email address'),
        '#maxlength' => 256,
        '#size' => 60,
      '#default_value' => array_column($recipients, 'email'),
    $form['disable_notifications'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Disable notifications'),
      '#description' => $this->t('Disables sending email notifications to recipients when new Meetings are created.'),
      '#default_value' => $config->get('disable_notifications'),
    return parent::buildForm($form, $form_state);

The array_column clinching it.

Thanks @sardara for a handy little module.