0

I have a block form with 1 radio button and one text field. The problem is the selection for the radio button is not saved: in blockSubmit $values['colour_select'] is empty. The value of the text box is saved and working fine. What is missing for the radio button?

Thank you

class UnitSiteIdentityBlock extends BlockBase implements BlockPluginInterface{
  public function build() {
    $config = $this->getConfiguration();
    $colour_select = $config['colour_select'];
    $custom_colour = $config['custom_colour'];
    return [
     '#theme' => 'folwell_unit_identity_block',
     '#colour_select' => $colour_select,
     '#custom_colour' => $custom_colour,
    ];
  }
  public function blockForm($form, FormStateInterface $form_state) {
    //$form = parent::blockForm($form, $form_state);
    $config = $this->getConfiguration();
    $form['colour_select'] = [
      '#type' => 'radios',
      '#title' => $this->t('Pick a colour'),
      '#options' => [
        'white' => $this->t('White'),
        'black' => $this->t('Black'),
        'other' => $this->t('Other'),
      ],
      '#attributes' => [
        'name' => 'field_select_colour',
      ],
      '#default_value' => $config['colour_select'],
    ];
    $form['custom_colour'] = [
      '#type' => 'textfield',
      '#size' => '60',
      '#placeholder' => 'Enter favourite colour',
      '#attributes' => [
        'id' => 'custom-colour',
      ],
      '#default_value' => $config['custom_colour'],
    ];
    return $form;
  }
  public function blockSubmit($form, FormStateInterface $form_state) {
    //parent::blockSubmit($form, $form_state);
    $values = $form_state->getValues();
    foreach ($values as $key => $value) {
      \Drupal::messenger()->addMessage($key . ': ' . $value);
    }
    $this->configuration['colour_select'] = $values['colour_select'];
    $this->configuration['custom_colour'] = $values['custom_colour'];
  }
}

1 Answer 1

0

I found the problem with this: it was the name attribute. As soon as I removed it, $values['colour_select'] stored the selected color.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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