drupal_get_form和drupal_retrieve_form有什么区别?


10

drupal_get_form()和之间有什么区别drupal_retrieve_form()?有人可以解释差异吗?我看的是API,但说明似乎与我相似。


我认为一个主要区别是,drupal_retrieve_form()允许您的代码访问$form_state通过引用传递的更新,但是drupal_get_form()只返回$form状态而不是状态。
菲利克斯·夏娃

Answers:


10

区别在于自动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'];
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.