hook_block_view没有传递信息

时间:2014-02-07 14:04:08

标签: drupal-7 block

我正在尝试在Drupal 7中构建我的第一个自定义模块。它是一个块表单,供用户在DB表中搜索客户信息。我创建了模块和信息文件。我的模块显示在模块和块部分下,但是当我将块添加到Content时,主题和内容不会从我的hook_block_view传递。因此,它只显示块标题和正文,而不是显示的表单。有人能告诉我我错过了什么吗?

<?php
/**
*@file
*
*/

/** Implements hook_block_info().
*
*/

function searchEngine_block_info(){
    $blocks = array();

    $blocks['searchEngine_form'] = array (
        'info'  => t("Applicant Search"),
        'cache' => DRUPAL_CACHE_GLOBAL,
    );
    return $blocks;
}

/** Implements hook_block_view().
*
*/

function searchEngine_block_view($delta = ''){
    $block = array();
    switch($delta) {
        case 'searchEngine_form':
        $block['subject'] = t('Applicant Search');
        $block['content'] = drupal_get_form('searchEngine_form');
    break;
    }
    return $block;
}

function searchEngine_form($form, &$form_state) {
    $form['searchOptions'] = array(
        '#type'     => 'select',
        '#title'    => t("Select how you would like to search for an applicant."),
        '#default_value'=> variable_get("gwf", true),
        '#options'  => array(
            'gwf'       => "GWF".t(" Number"),
            'email'     => t("Email"),
            'name'      => t("Name"),
            'phone_number'  => t("Phone Number"),
        ),

    );
    $form['data'] = array(
        '#type'     => 'textfeild',
        '#required' => TRUE,
    );
    $form['submit'] = array(
                '#type' => 'submit',
                '#value' => t('Submit'),
        );
        return $form;
}

function searchEngine_submit($form, $form_state) {
    if(isset($form['data'])){
        if($form['searchOptions'] == "name"){
            $name = preg_split("/[\s,]+/", $form['data']);
            $result = db_query('SELECT * FROM tls_active_applicants WHERE first_name = '.$name['0'].' AND last_name = '.$name['1']);
        }else{
            $result = db_query('SELECT * FROM tls_active_applicants WHERE '.$form['searchOptions'].' = '.$form['data']);
        }
        print_r($result);
    }
}

1 个答案:

答案 0 :(得分:0)

在这里传递可渲染数组很好:

$block['content'] = drupal_get_form('searchEngine_form');

我刚刚测试了您的代码,表单对我来说很合适:

enter image description here

现在我们知道代码的工作原理让我想知道它是不是只是某种css或隐藏它的东西?

我还会安装devel模块,因为它有助于调试。

您可以使用此代码:

function searchEngine_block_view($delta = ''){
    $block = array();
    switch($delta) {
        case 'searchEngine_form':
        $block['subject'] = t('Applicant Search');
        $form = drupal_get_form('test_form');
        dpm($form); // call to dpm here to log if you are successfully getting the form at this point
        $block['content'] = $form;
    break;
    }
    return $block;
}