使用form的pre_render属性,调用方法
参考以下例子
/**
* hook_form_alter
*/
function mymodule_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'mymodule_node_form') {
$form['field_part_1']['#pre_render'] = array('field_part_1_readonly');
$form['field_part_2']['#pre_render'] = array('field_part_2_disabled');
$form['field_part_3']['#pre_render'] = array('field_part_3_sort');
}
}
/**
* Helper function to readonly the element.
*/
function field_part_1_readonly($element) {
$element[0]['value']['#attributes'] = array('readonly' => 'readonly', 'class' => 'readonly');
return $element;
}
/**
* Helper function to disable the element.
*/
function field_part_2_disabled($element) {
$element['nid']['nid']['#attributes'] = array('disabled' => 'disabled');
return $element;
}
/**
* Helper function to ksort the element.
*/
function field_part_3_sort($element) {
ksort($element['nid']['nid']['#options']);
return $element;
}
*/
function mymodule_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'mymodule_node_form') {
$form['field_part_1']['#pre_render'] = array('field_part_1_readonly');
$form['field_part_2']['#pre_render'] = array('field_part_2_disabled');
$form['field_part_3']['#pre_render'] = array('field_part_3_sort');
}
}
/**
* Helper function to readonly the element.
*/
function field_part_1_readonly($element) {
$element[0]['value']['#attributes'] = array('readonly' => 'readonly', 'class' => 'readonly');
return $element;
}
/**
* Helper function to disable the element.
*/
function field_part_2_disabled($element) {
$element['nid']['nid']['#attributes'] = array('disabled' => 'disabled');
return $element;
}
/**
* Helper function to ksort the element.
*/
function field_part_3_sort($element) {
ksort($element['nid']['nid']['#options']);
return $element;
}
没有评论:
发表评论