2014-02-07 96 views
0

我想在Drupal 7中構建我的第一個自定義模塊。它是用戶搜索數據庫表的客戶信息的塊表單。我已經創建了模塊和信息文件。我的模塊顯示在模塊和塊的下面,但是當我將該塊添加到內容時,主題和內容不會從我的hook_block_view傳遞。所以,而不是顯示的形式,它只顯示塊的標題和正文。有人能告訴我我錯過了什麼嗎?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); 
    } 
} 

回答

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; 
} 
+0

這沒有奏效。進行更改和刷新後,我沒有注意到任何更改。 – slpcc63

+0

我剛剛測試過你的代碼,它對我來說工作正常。您需要更改的一件事是將您的函數searchEngine_submit重命名爲searchEngine_form_submit以使其正常工作。 –

+0

那麼你認爲這可能是與安裝Drupal的問題?唯一出現在我的是配置中的「塊標題」和「塊體」中的內容。我試着讓這些空的東西看看我的表單是否會顯示身體是否存在,但身體是必需的。有人提到我可能想要添加一個hook_block_configure,但我沒有這樣的運氣。任何其他想法? – slpcc63