2011-08-05 68 views
1

我感覺我必須真的很接近,但是從正確渲染出我的第一個Drupal模塊的一點點(儘管當然下一步是通過ajax提交和處理表單,猜測我不是很完美在樹林裏)如何讓我的模塊的模板文件在Drupal 7中呈現?

不管怎樣,從「Pro Drupal Development 7」開始,在堆棧上閱讀和詢問一些問題,我已經到了設置Drupal模塊的地步。

\sites\all\modules\custom\mymodule 
    ---- mymodule.info 
    ---- mymodule.css 
    ---- mymodule.js 
    ---- mymodule.tpl 
    ---- mymodule.module 

所有的要求似乎能正常運行,以獲得模塊在我的名單塊的CSS,JS &在頁面上可見,如果我選擇查看源。

爲了測試目的,我的TPL文件只包含以下內容。

<h1> HERE </h1> 

模塊文件(根據我的理解,這是我寫我的主題鉤子的地方)有以下(從我收集的)主要功能。

function mymodule_theme($existing, $type, $theme, $path){ 
    return array(
    'mymodule' => array(
     'variables' => array('content' => "FOO"), 
     'file'  => 'mymodule', 
     'path'  => drupal_get_path('module', 'mymodule') 
    ) 
); 
} 

function mymodule_page(){ 

    $content = ""; 
    return theme('mymodule', $content); 
} 

我很確定我只是有一個小的,相對簡單的問題 - 但一直在我自己的一個小時內鑿去。有誰知道答案,我在這裏錯過了什麼?

謝謝!

回答

3

嘿任何人誰認爲這在未來......事實證明,呈現一個視圖頁面在Drupal的功能<modulename>_block-view該塊的內容是呈現的內容。

function mymodule_block_view($delta = '') { 
$build['mymodule'] = array(
    '#theme' => 'mymodule', 
    '#title' => "FOO" 
); 

$block['subject'] = t('Title of second block (example_empty)'); 
// $block['content'] = drupal_render($build); // this was wrong 
$block['content'] = $build; 

return $block; 

} 

我發現從網上下載,並通過代碼

http://drupal.org/project/examples

具體步進解壓並安裝此http://ftp.drupal.org/files/projects/examples-7.x-1.x-dev.zip/sites/all/modules,你可以看到你自己的Drupal站點活生生的例子例子瀏覽代碼。

注:從NMC下面是在正確的答案模塊文件必須被重命名爲mymodule.tpl.php

編輯按照下面的評論,是不需要drupal_render呼叫註釋掉線。它在一個環境中工作,但不在另一個環境中工作。最好不要做到一致。

+0

這是不正確的。你不需要drupal_render塊的內容。 – chx

+0

謝謝,我糾正了。 –

1

嘗試

function mymodule_theme($existing, $type, $theme, $path){ 
    return array(
    'mymodule' => array(
     'variables' => array('content' => "FOO"), 
     'template' => 'mymodule', 
    ) 
); 
} 

和重命名你的mymodule.tplmymodule.tpl.php

+0

感謝您的回答,仍然缺少一些東西。 –