2010-04-14 123 views
1

我正在写一个自定义模块,我想在另一个非form 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 
} 

回答

1

PHP函数都没有意识到,在其他功能有什么变数。

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

+0

googletorp,感谢您的回答。我最终定义了一个全局变量。 – logii 2010-04-16 13:43:37

+0

我经常使用静态类对象,这使您在命名全局变量和特定于站点的功能时更加自由。它还可以避免在需要访问的每个函数中使用'global $ whatever'。 – artfulrobot 2012-03-23 18:27:07

0

试试这个:

$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

// 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);