Skip to content

Instantly share code, notes, and snippets.

@underhilllabs
Created March 17, 2014 20:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save underhilllabs/9607418 to your computer and use it in GitHub Desktop.
Save underhilllabs/9607418 to your computer and use it in GitHub Desktop.
Build an array of checkboxes in drupal #drupal.

Build up array of elements

  $query = db_select("users_roles", "ur")
             // TODO: fix hardcoded variable 5 for supervisor role
             ->condition("rid", 5)
             ->fields("ur", array("uid"));
  $result = $query->execute();

  $results_arr = array();
  foreach($result as $row) {
    $u = user_load($row->uid);
    $results_arr[$u->uid] = $u->name;
  }

next use that array results_arr as the #options in checkboxes element

  $form['supervisors'] = array(
    '#title' => 'Select Supervisors to add to Session',
    '#type' => 'checkboxes',
    '#options' => $resultsarr,
  );

in form submit callback grab the checked values

$sup_ids = array_filter($form_state['values']['supervisors']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment