Answers:
区别在于自动drupal_get_form()
处理,处理和显示模块的渲染HTML表单,同时drupal_retrieve_form()
返回定义表单的结构化数组。
drupal_get_form()
首先使用form_get_cache()检查表单是否存在于表单缓存中;如果不存在,则调用drupal_retrieve_form()和drupal_prepare_form()。
drupal_get_form()
调用drupal_process_form(),该函数调用drupal_validate_form(),并使用form_execute_handlers()调用提交处理程序。
渲染是从开始的drupal_process_form()
,调用form_builder()。
在大多数情况下,drupal_get_form()
就是要使用的函数,例如以下代码。
function node_menu() {
$items['admin/content'] = array(
'title' => 'Content',
'description' => 'Find and manage content.',
'page callback' => 'drupal_get_form',
'page arguments' => array('node_admin_content'),
'access arguments' => array('access content overview'),
'weight' => -10,
'file' => 'node.admin.inc',
);
// …
return $items;
}
我发现drupal_retrieve_form()
直接调用的唯一模块是mollom_moderate()中的Mollom模块,该模块使用该模块自动提交表单。该代码可以使用drupal_form_submit(),但是(如评论所示),“程序化表单提交不能自动使用主表单提交按钮/操作,因此我们需要类似drupal_form_submit()
。”
$form_id = $form_info['delete form'];
$form_state = form_state_defaults();
// We assume that all delete confirmation forms take the fully loaded
// entity as (only) argument.
$messages[] = "Attempt to load $form_info[entity] entity via entity_load().";
$entities = entity_load($data->entity, array($data->id));
$form_state['build_info']['args'][] = $entities[$data->id];
$form = drupal_retrieve_form($form_id, $form_state);
$form_state['values'] = array();
$form_state['values']['mollom']['feedback'] = '';
// Take over the primary submit button of confirm_form().
$form_state['values']['op'] = $form['actions']['submit']['#value'];
$form_state['input'] = $form_state['values'];
$form_state['programmed'] = TRUE;
// Programmed forms are always submitted.
$form_state['submitted'] = TRUE;
// Reset form validation.
$form_state['must_validate'] = TRUE;
form_clear_error();
drupal_prepare_form($form_id, $form, $form_state);
drupal_process_form($form_id, $form, $form_state);
$result = $form_state['executed'];
drupal_retrieve_form()
允许您的代码访问$form_state
通过引用传递的更新,但是drupal_get_form()
只返回$form
状态而不是状态。