Drupal theme checkboxes

my_module.module

<?php
/**
 * Implements hook_theme(); 
 */
function my_module_theme() {
  return array(
    '_my_module_custom_form' => array(
      'render element' => 'form',
      'template' => 'my-module-custom-form',
    ),
    'my_module_form_element' => array(
      'render element' => 'element',
    ),
  );
}
 
function _my_module_custom_form($form, &$form_state) {
 
  $form['type'] = array(
    '#type' => 'checkboxes',
    '#required' => FALSE,
    '#tree' => TRUE,
    '#options' => array(
      'one' => 'This week',
      'two' => 'Last week',
    ),
    '#default_value' => array(0, 0),
    '#theme_wrappers' => array('my_module_form_element'),
  );
 
  $form['type']['one']['#theme_wrappers'] = array('my_module_form_element');
  $form['type']['two']['#theme_wrappers'] = array('my_module_form_element');
 
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );
 
  return $form;
}
 
function theme_my_module_form_element($variables) {
  $element = $variables['element'];
  $output = $element['#children'] . "\n";
  return $output;
}
 
?>

my-module-custom-form.tpl.php

<ul>
  <li class="checkbox"><label for="one"><?php print render($form['type']['one']); ?>
     Option One</label></li>
  <li class="checkbox"><label for="two"><?php print render($form['type']['two']); ?>
     Option Two</label></li>
</ul>
 
<p class="buttons"><?php print render($form['submit']); ?></p>
<?php hide($form['type']); ?>
<?php print drupal_render_children($form); ?>

Tags

Internal References

External References

Article Type

General