php - 如何在 FAPI 函数之外获取 $form_state?

标签 php drupal forms drupal-6 module

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

感谢任何帮助:)

<?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
}

最佳答案

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

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

关于php - 如何在 FAPI 函数之外获取 $form_state?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2641683/

相关文章:

drupal - Drupal 5 中的批处理节点操作

php - 没有错误,但代码不起作用

javascript - 使用 firestore 的 Angular 异步验证器

javascript - 使用 BUTTON 元素在 ReactJS 中提交表单

php - 你如何在没有多个数据库连接的情况下执行多个 AJAX 请求?

PHP 6 全局变量已弃用?

PHP XML-导出为文件下载

drupal - 在单独的选项卡上显示评论

php - Laravel 在主页上显示 index.php 的代码

php - Laravel 数据库优先() "Trying to get property of non-object"