欢迎各位兄弟 发布技术文章
这里的技术是共享的
Assuming that you're comfortable with creating a Drupal form, you can create a Drupal form checkbox (technically a Drupal checkboxes element) as easily as this:
# an html checkbox for our drupal form # the options to display in our checkboxes $toppings = array( 'pepperoni' => t('Pepperoni'), 'black_olives' => t('Black olives'), 'veggies' => t('Veggies') ); # the drupal checkboxes form field definition $form['pizza'] = array( '#title' => t('Pizza Toppings'), '#type' => 'checkboxes', '#description' => t('Select the pizza toppings you would like.'), '#options' => $toppings, );
The checkbox is specified in this line of code:
'#type' => 'checkboxes',
This Drupal checkboxes form field definition renders an HTML checkbox field that looks like this:
Note: I originally wrote this tutorial for Drupal 6, and I just confirmed that it works for Drupal 7 as well.
(If you're not comfortable with creating a Drupal module or Drupal form, please see my simple Drupal form example/tutorial.)
You can add even more fields to your Drupal checkbox definition. The fields you can add are defined in the grid at this Drupal Form API reference page.
I hope this Drupal checkbox form example has been helpful. If you have any questions just leave a note in the Comments section below and I'll be glad to help.
来自 http://alvinalexander.com/drupal/drupal-checkboxes-form-syntax-examples-tutorial
$options = array('A', 'B', 'C');
foreach ($themas as $thema) {
// Initialize array
$ra = array();
// Fill up the array with different keys
$key = $prefix.'_thema_'.$thema->tid.'_fiche';
$ra[$key]['#type'] = 'checkboxes';
$ra[$key]['#name'] = $prefix.'_thema_'.$thema->tid.'_opties';
$ra[$key]['#options'] = $options;
}
正确答案
I think it's because you're re-initialising
$ra
in every step of the loop so it will only ever contain one set of checkboxes. Try initialising it outside of the loop:
$options = array('A', 'B', 'C');
// Initialize array
$ra = array();
foreach ($themas as $thema) {
// Fill up the array with different keys
$key = $prefix.'_thema_'.$thema->tid.'_fiche';
$ra[$key]['#type'] = 'checkboxes';
$ra[$key]['#name'] = $prefix.'_thema_'.$thema->tid.'_opties';
$ra[$key]['#options'] = $options;
}
$form['some_key'] = $ra;
Also make sure your
$prefix
string doesn't start with a #
symbol or Drupal will consider it a property rather than an element that needs to be rendered.
It was indeed an initializing issue! The value #options
got overwritten every time.
function login_disable_settings_form($form, &$form_state) {
$form = array();
$form['all_users'] = array(
'#type' => 'fieldset',
'#title' => t('All Users'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['all_users']['login_disable_is_active'] = array(
'#type' => 'checkbox',
'#title' => 'Prevent user log in',
'#description' => t('When active the user login form will be disabled for everyone. For roles granted bypass rights they must use the access key defined below.'),
'#default_value' => (bool)variable_get('login_disable_is_active', FALSE),
'#states' => array(
'unchecked' => array(
':input[name="login_disable_individual"]' => array('checked' => TRUE),
),
),
);
$form['all_users']['login_disable_key'] = array(
'#title' => t('Access key (optional)'),
'#description' => t('For added security, a word can be required to be added to the URL.'),
'#type' => 'textfield',
'#size' => 10,
'#default_value' => variable_get('login_disable_key', 'admin'),
);
if (!empty($form['login_disable_key']['#default_value'])) {
$form['login_disable_key']['#description'] .= '<br />' . t('The URL to use to log in is: @url', array('@url' => url('user/login') . '?' . $form['login_disable_key']['#default_value']));
}
$form['all_users']['login_disable_message'] = array(
'#title' => t('End-user message when login is disabled'),
'#description' => t('The message to display to users when login has been disabled.'),
'#type' => 'textfield',
'#size' => 80,
'#default_value' => variable_get('login_disable_message', t('Member access has been temporarily disabled. Please try again later.')),
);
$form['individual'] = array(
'#type' => 'fieldset',
'#title' => t('Indivdual'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['individual']['login_disable_individual'] = array(
'#type' => 'checkbox',
'#title' => t('Disable on user profile'),
'#description' => t('Checking this box allows disabling individual logins on user profile pages.'),
'#states' => array(
'unchecked' => array(
':input[name="login_disable_is_active"]' => array('checked' => TRUE),
),
),
);
$form = system_settings_form($form);
return $form;
}
来自 http://drupal.stackexchange.com/questions/136957/undesired-toggle-effect-with-states-and-checkboxes