2010-04-14 3 views
1

커스텀 모듈을 작성 중이며, 다른 비 형식 API 함수 인 -> custom_facet_view_build()에서 현재 form의 $ 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 변수를 전역 변수에 저장할 수 있습니다. 그렇지 않으면 변수를 db에 저장해야합니다. 그것이 HTTP의 고통인데, 이는 상태없는 시스템입니다.

+0

googletorp, 대답을 주셔서 감사합니다

$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를 제공함으로써

. 전역 변수 정의가 끝났습니다. – logii

+0

저는 정적 변수 객체를 사용하기 때문에 전역 변수와 사이트 별 함수의 이름을 자유롭게 지정할 수 있습니다. 또한 액세스가 필요한 모든 함수에서'global $ whatever'를 사용하지 않아도됩니다. – artfulrobot

관련 문제