首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在FAPI的函数之外获取$form_state?

如何在FAPI的函数之外获取$form_state?
EN

Stack Overflow用户
提问于 2010-04-15 07:12:11
回答 2查看 2.3K关注 0票数 2

我正在编写一个自定义模块,并且我想在另一个非form api函数-> custom_facet_view_build()中使用当前表单的$form_state。

如有任何帮助,我们将非常感谢:)

代码语言:javascript
复制
<?php
/**
 * Implementation of hook_perm().
 */
function custom_facet_perm() {
  return array(
    'access foo content',
    'access baz content',
  );
}

/**
 * Implementation of hook_menu().
 */
function custom_facet_menu() {
  $items['faceted-search'] = array(
    'title' => 'Faceted Search',
    'page callback'    => 'drupal_get_form',
    'access arguments' => array(),
  );

  $items['facet-search-test'] = array(
    'page callback'    => 'drupal_get_form',
    'page arguments'   => array('custom_facet_form'),
    'access callback'  => TRUE,
    'type'             => MENU_CALLBACK,
  );

  return $items;
}

/**
 * Form definition; ahah_helper_demo form.
 */
function custom_facet_form($form_state) {
  $form = array();

  ahah_helper_register($form, $form_state);

  if (isset($form_state['storage']['categories'])) {
    $categories_default_value = $form_state['storage']['categories']["#value"];
  }

  $form['facet_search_form'] = array(
    '#type'   => 'fieldset',
    '#title'  => t('Faceted Search'),
    '#prefix' => '<div id="billing-info-wrapper">', // This is our wrapper div.
    '#suffix' => '</div>',
    '#tree'   => TRUE, // Don't forget to set #tree!
  );

  $form['facet_search_form']['categories'] = array(
    '#type' => 'select',
    '#title' => t('Category'),
    '#options' => _custom_facet_taxonomy_query(1),
    '#multiple' => TRUE,
    '#default_value' => $categories_default_value,
  );

  $form['save'] = array(
    '#type'  => 'submit',
    '#value' => t('Save'),
  );

  return $form;
}

/**
 * Validate callback for the form.
 */
function custom_facet_form_validate($form, &$form_state) {

}

/**
 * Submit callback for the form.
 */
function custom_facet_form_submit($form, &$form_state) {
  drupal_set_message('nothing done');
  $form_state['storage']['categories'] = $form['facet_search_form']['categories'];
  // dpm($form_state); // There's a value returned in form_state['storage] within this function
}

/**
 * Implementation of hook_views_api().
 */
function custom_facet_views_api() {
  return array(
    'api' => 2,
  );
}

function custom_facet_view_build(&$view) {
    dpm($form_state); // form_state['storage] remains NULL even though there's a value on previous submission
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-04-15 14:58:12

PHP函数不知道其他函数中有哪些变量。

如果在同一请求周期内调用这些函数,则可以将$form_state变量存储在全局变量中。否则,您将需要将该变量存储在数据库中。这就是HTTP的痛苦所在,它是一个无状态系统。

票数 2
EN

Stack Overflow用户

发布于 2016-08-26 17:18:53

试试这个:

代码语言:javascript
复制
 $form_state = form_state_defaults();
 $form_build_id = $_POST['form_build_id'];
 // Get the form from the cache.
 $form = form_get_cache($form_build_id, $form_state);

或者通过提供表单id:

致词:https://drupal.stackexchange.com/questions/158408/how-do-i-load-the-form-state-for-a-form-loaded-with-drupal-get-form/175884#175884

代码语言:javascript
复制
// Get the form.
$form = drupal_get_form('my_form_id');
// Get the default form state.
$form_state = form_state_defaults();
// Get the form and form state from the cache for the form you just got.
form_get_cache($form['#build_id'], $form_state);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2641683

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档