-2

Maybe I'm reading documentation wrong. Shouldn't this code:

$form['packet_pickup_checkboxes'] = [
   '#type'=>'checkboxes',
   '#options' => [
     'self' => 'I will pick up my own packets',
     'other' => 'The following person will pick up my packet',
   ],
   '#default_value' => ['self'],
   '#multiple' => FALSE,
 ];

Result in a checkboxes array which only allows one selection? I ended up writing some custom jQuery to make this happen.

The reason for not using radio buttons is that Drupal 8 radio buttons are not accessible and we're required to have fully accessible forms.

In case you also need this to work here's some code, this code also hides the pick up person's name if "self" is chosen:

$form['jquery_include'] = [
  '#type' => 'html_tag',
  '#tag' => 'script',
  '#attributes' => [
    'src' => 'https://code.jquery.com/jquery-3.4.1.min.js',
    'integrity' => "sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=",
    'crossorigin' => "anonymous"
  ],
 ];
 $form['packet_pickup_default_script'] = [
  '#type' => 'html_tag',
  '#tag' => 'script',
  '#value' => "
    $(document).ready(function()
    {
      var regnum={$regnum};
      var self_cbx = $('#edit-packet-pickup-person-self-'+regnum);
      var other_cbx = $('#edit-packet-pickup-person-other-'+regnum);
      var other_fname = $('.js-form-item-packet-pickup-first-name-'+regnum);
      var other_lname = $('.js-form-item-packet-pickup-last-name-'+regnum);
      self_cbx.prop('checked', true);
      other_fname.hide();
      other_lname.hide();
      self_cbx.click(function(){
        self_cbx.prop('checked',true);
        other_cbx.prop('checked',false);
        other_fname.hide()
        other_lname.hide();
      });
      other_cbx.click(function(){
        other_cbx.prop('checked',true);
        self_cbx.prop('checked',false);
        other_fname.show();
        other_lname.show();
      });
    });
  ",
];
$form["packet_pickup_person_self_{$regnum}"] = [
  '#type' => 'checkbox',
  '#value' => 'self',
  '#title' => $this->t('I will pick up my packet myself'),
];
$form["packet_pickup_person_other_{$regnum}"] = [
  '#type' => 'checkbox',
  '#value' => 'other',
  '#title' => $this->t('The following person will pick up my packet for me.'),
];

$form["packet_pickup_first_name_{$regnum}"] = [
  '#type' => 'textfield',
  '#title' => $this->t('First Name'),
  '#description' => $this->t("Enter pickup person's first name."),
  '#default_value' => $form_state->getValue("registrant_first_name_{$regnum}", ''),
  '#required' => FALSE,
  '#size' => 24,
  '#maxlength' => 24,
];
$form["packet_pickup_last_name_{$regnum}"] = [
  '#type' => 'textfield',
  '#title' => $this->t('Last Name'),
  '#description' => $this->t("Enter pickup person's last name."),
  '#default_value' => $form_state->getValue("registrant_last_name_{$regnum}", ''),
  '#required' => FALSE,
  '#size' => 24,
  '#maxlength' => 24,
];

I had to use $regnum in the jquery script. A function call that did this (so I could attach the javascript as an external library) resulted in a chicken-before-the-egg issue where you had to click the checkboxes first before the script started working. That was too wonky for my taste. I wanted them to behave the very first time I clicked them.

Anyone have a cleaner way to do this with checkboxes?

put on hold as unclear what you're asking by leymannx, Jdrupal, mradcliffe, Yusef, kiamlaluno Nov 14 at 10:49

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Based on this D7 based table "multiple" is only for use with select and tableselect api.drupal.org/api/drupal/… I'd imagine it's the same for D8. Where did you see it can be used on checkboxes? – Leigh Nov 4 at 16:51
  • 4
    I am wondering what you mean by "radios are not accessible"? – leymannx Nov 4 at 18:04

Browse other questions tagged or ask your own question.