2

I want to use a '#type' => 'checkboxes' render array to display some checkboxes on the page. They aren't going to be part of a form that ever gets submitted, as they are purely for browser-side user interaction. So I don't need any of the extra functionality provided by drupal_get_form(). I just need a few <input type="checkbox"> elements.

Unfortunately, Drupal appears to simply refuse to render '#type' => 'checkboxes' render arrays unless you pass the array through drupal_get_form(). When I try, absolutely nothing except

<div class="form-item form-type-checkboxes">
 <label>Landmarks</label>
 <div class="form-checkboxes"></div>
</div>

gets rendered.

Is there any way to get Drupal to render an <input type="checkbox"> element without using drupal_get_form()?

3

...I just need a few <input type="checkbox"> elements.

Just render it as plain html, and if you absolutely want a render array, then create your own theme function for it.

$variables['content']['checkboxes'] = array(
    '#markup' => '<div class="display"> Please select every sport that you play.<br> Soccer: <input type="checkbox" name="sports" value="soccer"><br> Football: <input type="checkbox" name="sports" value="football"><br> Baseball: <input type="checkbox" name="sports" value="baseball"><br> Basketball: <input type="checkbox" name="sports" value="basketball"><br>
</div>'
);
  • This is ultimately what I went with, but it just feels icky. I would really have liked for there to be a way to convert a render array into a set of checkboxes. – coredumperror Feb 12 '15 at 22:08
2

use this tested code

$element = array(
 '#type' => 'checkboxes',
 '#title' => 'test',
 '#options' => drupal_map_assoc(array(t('SAT'), t('ACT'))),
 '#parents' => array(),
);

$form_state = array('values' => array());
$form_Build = form_builder('just_test', $element, $form_state);
print drupal_render($form_Build);
  • Unfortunately, that still renders all the extra Drupal form cruft that I don't want. I just want a set of <input type="checkboxes"> elements and associated <label>s. – coredumperror Feb 9 '15 at 22:34
  • Core has no such function @CoreDumpError. The closest you'll get is theme_checkboxes, but even that has a hard-coded div wrapper (plus you'll probably need to build the elements manually anyway). You could always use theme_checkbox in a loop – Clive Feb 9 '15 at 23:29
  • Yeah, I figured as much. Thanks for confirming that I'll have to do this manually. It's ugly, but it works. – coredumperror Feb 10 '15 at 0:03
1

I believe this doesn't work because form_process_checkboxes isn't called if you don't run it through the form API.

Before you drupal_render your element, try running it through form_process_checkboxes manually.

$form['yourCheckboxes'] = form_process_checkboxes($form['yourCheckboxes']);
print drupal_render($form);
0

An example of code :

$accounts = user_load_multiple(array_keys($values));
foreach($values as $uid => $value) {
  $element[$delta][$uid] = array(
    '#type' => 'checkbox',
    '#title' => format_username($accounts[$uid]),
    '#checked' => !empty($value),
    '#attributes' => array('disabled' => 'disabled'),
  );
}

Later (may not be needed in your code):

drupal_render($element);
0

Have you looked at theme_checkbox? Unfortunately, it only renders the checkbox, so you'd have to add the labels yourself. But if you're really looking for a Drupal-esque solution, this could be viable.

Example:

<?php

$element = array(
  '#id' => 'checkbox-id',
  '#name' => 'checkbox-name',
);

echo '<label for="checkbox-id">Checkbox</label>'. theme('checkbox', array('element' => $element)); 

?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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