简体   繁体   English

自定义Drupal搜索模块的表单在提交时会丢失所有帖子数据

[英]custom drupal search module's form losing all post data when submitted

I am modifying an already contributed drupal module (Inline Ajax Search) to handle searching of a specific content type with some search filters (ie when searching for help documentation, you filter out your search results by selecting for which product and version of the product you want help with). 我正在修改一个已经提供的drupal模块(内联Ajax搜索),以使用一些搜索过滤器来处理特定内容类型的搜索(即,在搜索帮助文档时,您可以通过选择要使用的产品和产品版本来过滤搜索结果需要帮助)。

I have modified the module some what to handle all the search filters. 我已经对该模块进行了一些修改,以处理所有搜索过滤器。

I also added in similar functionality from the standard core search module to handle the presenting of the search form and search results on the actual search page ( not the block form ). 我还从标准核心搜索模块中添加了类似的功能,以处理搜索表单的显示和实际搜索页面(而不是阻止表单)上的搜索结果。

The problem is that when i submit the form, i discovered that I'd lose all my post data on that submit because somewhere, and i don't know where, drupal is either redirecting me or something else is happening that is causing me to lose everything in the $_POST array. 问题是,当我提交表单时,我发现我丢失了该提交中的所有帖子数据,因为某个地方,而且我不知道在哪里,drupal要么重定向我,要么其他事情正在发生,导致我失去$ _POST数组中的所有内容。

here's the hook_menu() implementation: 这是hook_menu()的实现:

<?php  
function inline_ajax_search_menu() {
    $items = array();
    $items['search/inline_ajax_search'] = array(  
        'title' => t('Learning Center Search'),
        'description' => t(''),
        'page callback' => 'inline_ajax_search_view',
        'access arguments' => array('search with inline_ajax_search'),
        'type' => MENU_LOCAL_TASK,
        'file' => 'inline_ajax_search.pages.inc',
    );
}
?>

the page callback is defined as such (very similar to the core search module's search_view function): 页面回调就是这样定义的(非常类似于核心搜索模块的search_view函数):

<?php  
function inline_ajax_search_view() {

    drupal_add_css(drupal_get_path('module', 'inline_ajax_search') . '/css/inline_ajax_search.css', 'module', 'all', FALSE );

    if (isset($_POST['form_id'])) {
        $keys = $_POST['keys'];

        // Only perform search if there is non-whitespace search term:
        $results = '';
        if(trim($keys)) {     
            require_once( drupal_get_path( 'module', 'inline_ajax_search' ) . '/includes/inline_ajax_search.inc' );

            // Collect the search results:
            $results = _inline_ajax_search($keys, inline_ajax_search_get_filters(), "page" );



            if ($results) { 
                $results = theme('box', t('Search results'), $results);
            }
            else {
                $results = theme('box', t('Your search yielded no results'), inline_ajax_search_help('inline_ajax_search#noresults', drupal_help_arg()));
            }
        }
        // Construct the search form.
        $output = drupal_get_form('inline_ajax_search_search_form', inline_ajax_search_build_filters( variable_get( 'inline_ajax_search_filters', array() ) ) );
        $output .= $results;

        return $output;
    }
  return drupal_get_form('inline_ajax_search_search_form', inline_ajax_search_build_filters( variable_get( 'inline_ajax_search_filters', array() ) ) );
}
?>

from my understanding, things should work like this: A user goes to www.mysite.com/search/inline_ajax_search and drupal will process the path given in my url and provide me with a page that holds the themed form for my search module. 以我的理解,事情应该是这样的:用户访问www.mysite.com/search/inline_ajax_search,drupal将处理我的url中给定的路径,并为我提供一个页面,其中包含用于我的搜索模块的主题表单。 When i submit the form, whose action is the same url (www.mysite.com/search/inline_ajax_search), then we go thru the same function calls, but we now have data in the $_POST array and one of them is indeed $_POST['form_id'] which is the name of the form "inline_ajax_search_search_form". 当我提交表单时,其动作是相同的url(www.mysite.com/search/inline_ajax_search),然后我们通过相同的函数调用进行操作,但是现在我们在$ _POST数组中有数据,其中一个确实是$ _POST ['form_id']是表单“ inline_ajax_search_search_form”的名称。 so we should be able to enter into that if block and put out the search results. 因此我们应该能够进入if阻止并推出搜索结果。

but that's not what happens...somewhere from when i submit the form and get my results and theme it all up, i get redirected some how and lose all my post data. 但这不是发生的事情...从我提交表单并获得结果和主题的某个地方开始,我就被重定向到某些方式,并丢失了所有我的帖子数据。

if anybody can help me, it'd make me so happy lol. 如果有人可以帮助我,那会让我很高兴。

drupal_get_form actually wipes out the $_POST array and so that's why I lose all my post data. drupal_get_form实际上消除了$ _POST数组,因此这就是我丢失所有发布数据的原因。

according to this: http://drupal.org/node/748830 $_POST should really be ignored when doing things in drupal. 据此: http : //drupal.org/node/748830 $ _POST在使用drupal时确实应该被忽略。 It's better to find a way around using it. 最好找到一种使用它的方法。 One way is the way described in the link, making ur form data persist using the $_SESSION array. 一种方法是链接中描述的方法,使您的表单数据使用$ _SESSION数组保留。 I'm sure there are various other and better ways to do this, but yeah, drupal_get_form was the culprit here... 我敢肯定还有其他更好的方法可以做到这一点,但是是的,drupal_get_form是这里的罪魁祸首...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM