Ecco il mio esempio (usando #after_build
):
$form['legal']['legal_accept']['#type'] = 'checkboxes';
$form['legal']['legal_accept']['#options'] = $options;
$form['legal']['legal_accept']['#after_build'][] = '_process_checkboxes';
Più la seguente funzione di richiamata:
function _process_checkboxes($element) {
foreach (element_children($element) as $key) {
if ($key == 0) { // value of your checkbox, 0, 1, etc.
$element[$key]['#attributes'] = array('disabled' => 'disabled');
// $element[$key]['#theme'] = 'hidden'; // hide completely
}
}
return $element;
}
Testato su Drupal 6, ma dovrebbe funzionare anche su Drupal 7.
Drupal 6
È possibile utilizzare la seguente funzione ( sorgente ):
/*
* Change options for individual checkbox or radio field in the form
* You can use this function using form_alter hook.
* i.e. _set_checkbox_option('field_tier_level', 'associate', array('#disabled' => 'disabled'), $form);
*
* @param $field_name (string)
* Name of the field in the form
* @param $checkbox_name (string)
* Name of checkbox to change options (if it's null, set to all)
* @param $options (array)
* Custom options to set
* @param $form (array)
* Form to change
*
* @author kenorb at gmail.com
*/
function _set_checkbox_option($field_name, $checkbox_name = NULL, $options, &$form) {
if (isset($form[$field_name]) && is_array($form[$field_name])) {
foreach ($form[$field_name] as $key => $value) {
if (isset($form[$field_name][$key]['#type'])) {
$curr_arr = &$form[$field_name][$key]; // set array as current
$type = $form[$field_name][$key]['#type'];
break;
}
}
if (isset($curr_arr) && is_array($curr_arr['#default_value'])) {
switch ($type) { // changed type from plural to singular
case 'radios':
$type = 'radio';
break;
case 'checkboxes':
$type = 'checkbox';
break;
}
foreach ($curr_arr['#default_value'] as $key => $value) {
foreach($curr_arr as $old_key => $old_value) { // copy existing options for to current option
$new_options[$old_key] = $old_value;
}
$new_options['#type'] = $type; // set type
$new_options['#title'] = $value; // set correct title of option
$curr_arr[$key] = $new_options; // set new options
if (empty($checkbox_name) || strcasecmp($checkbox_name, $value) == 0) { // check name or set for
foreach($options as $new_key => $new_value) {
$curr_arr[$key][$new_key] = $value;
}
}
}
unset($curr_arr['#options']); // delete old options settings
} else {
return NULL;
}
} else {
return NULL;
}
}
/*
* Disable selected field in the form(whatever if it's textfield, checkbox or radio)
* You can use this function using form_alter hook.
* i.e. _disable_field('title', $form);
*
* @param $field_name (string)
* Name of the field in the form
* @param $form (array)
* Form to change
*
* @author kenorb at gmail.com
*/
function _disable_field($field_name, &$form) {
$keyname = '#disabled';
if (!isset($form[$field_name])) { // case: if field doesn't exists, put keyname in the main array
$form[$keyname] = TRUE;
} else if (!isset($form[$field_name]['#type']) && is_array($form[$field_name])) { // case: if type not exist, find type from inside of array
foreach ($form[$field_name] as $key => $value) {
if (isset($form[$field_name][$key]['#type'])) {
$curr_arr = &$form[$field_name][$key]; // set array as current
break;
}
}
} else {
$curr_arr = &$form[$field_name]; // set field array as current
}
// set the value
if (isset($curr_arr['#type'])) {
switch ($curr_arr['#type']) {
case 'textfield':
default:
$curr_arr[$keyname] = TRUE;
}
}
}