2012-01-04 39 views
0

我已經創建了一個模塊,它從用戶指定爲「收藏夾」的節點獲取註釋。所以我並不試圖輸出所有節點的所有評論,比如最近的評論塊,但只是來自指定爲「最愛」的節點。使用現有的來自Drupal模塊的comment.tpl.php列出節點註釋

查詢全部正常,我通過打印來自不同對象的值來測試這個問題。所以我得到了每個評論和相應節點對象的整個評論對象。我已經能夠創建CID,NID,註釋文本等,並輸出這些名單與

$block['content'] = theme('item_list', array('items' => $items)); 

,但我怎麼會去渲染在相同的佈局,我我的模塊中得到了評論的對象/設計,因爲我在我的節點頁面上?我的節點頁面上的評論是用我自己的佈局/設計設置的comment.tpl.php文件呈現的,我希望我的模塊以同樣的方式呈現這些評論。

所以這是我hook_block_view()實現,我相信這是從一個模塊輸出的正確方法:

function jf_comment_feed_block_view($delta = '') { 
switch($delta){ 
    case 'jf_comment_feed': 
     $block['subject'] = t('Comment feed'); 
     if(user_access('access content')){ 
      // Get users favourite locations 
      $loc_result = jf_comment_feed_locations(); 

      $fav_loc = array(); 
      foreach ($loc_result as $loc) { 
       $fav_loc[] = array(
        'data' => $loc->nid, 
       ); 
      } 

      if (empty($fav_loc)) { //No content in the last week. 
       $block['content'] = t('No favourite locations added. 
        To see what goes on at your favourite locations add locations to 
        +My Locations and the posts from those locations will show here.'); 
      } else { 
       //Use our custom function to retrieve data. 
       $result = jf_comment_feed_contents($fav_loc); 

       // ############################################ 
       // Here I need to create my output... I think... 

       // Previously I rendered my results from the query 
       // by using this code (this prints the comment id): 
       // $items = array(); 
       // foreach ($result as $comment){ 
       // $items[] = array(
       //  'data' => comment_load($comment->cid), 
       // ); 
       // } 
       // ############################################ 

       if (empty($items)) { //No content in the last week. 
        $block['content'] = t('No posts from last week.'); 
       } else { 
        // This is the code used to render the 
        // comment id to the block: 
        // $block['content'] = theme('item_list', array('items' => $items)); 
       } 
      } 
     } 
} 
return $block; 
} 

我也試過用:

$block['content'] = theme('comment_view', $mycomment, $mynode); 
$block['content'] = theme('comment', $mycomment, $mynode); 

其中$ mycomment是comment對象和$ mynode是節點對象。但是這打破了頁面。

當然,我必須在這裏失去一行代碼,但我現在用了兩天的時間搜索這個並沒有運氣......所以,感謝任何幫助。

編輯 @Clive確實觸發了一些想法,我嘗試根據節點頁面上數組的樣子創建自己的數組。我用Devel Themer info模塊獲得了陣列的結構和名稱。

這個數組輸出評論創建者的用戶圖片和日期,但我在我的評論中添加了一個自定義字段field_jf_comment,雖然我可以在Devel中看到數組中的信息,但並未顯示。我不使用標準的開箱即用評論字段,因爲我需要一個文本字段而不是可擴展的textarea輸入。一個設計決定。

現在顯然這並不理想,因爲我手動設置了大部分值。這適用於我當前的項目,但如果模塊更通用一點,那麼它會很酷,所以其他人也可以使用它。當我用Devel Themer信息點擊我的節點頁面上的單個評論時,我得到一個包含元素,數組項目的用戶對象和數組項目,比如db_is_active,is_admin等等。如果我可以以某種方式重新創建此數組,然後將此數組設置爲$ block ['content'],我相信這會起作用。

這裏的數組的實現:

foreach ($result as $comment) { 
    $items[] = array(
    '#entity_type' => 'comment', 
    '#bundle' => 'comment_node_location', 
    '#theme' => 'comment__node_location', 
    '#comment' => comment_load($comment->cid, FALSE), 
    '#node' => node_load($comment->nid), 
    '#view_mode' => 'full', 
    'field_jf_comment' => array(
     '#theme' => 'field', 
     '#title' => 'Title', 
     '#access' => TRUE, 
     '#label_display' => 'above', 
     '#view_mode' => 'full', 
     '#language' => 'und', 
     '#field_name' => 'field_jf_comment', 
     '#field_type' => 'text', 
     '#entity_type' => 'comment', 
     '#bundle' => 'comment_node_location', 
     '#items' => array(
     '0' => array(
      // This isn't working and gives an error saying: 
      // Notice: Undefined property: stdClass::$field_jf_comment in 
      // jf_comment_feed_block_view() 
      'value' => $comment->field_jf_comment['und']['0']['value'], 
      'format' => $comment->field_jf_comment['und']['0']['format'], 
      'safe_value' => $comment->field_jf_comment['und']['0']['safe_value'] 
     ) 
    ) 
    ) 
); 
} 

而且我得到它呈現:

$block['content'] = $items; 

編輯 @Clive是正確的。他的代碼和我的代碼一樣,但代碼少。並有一些修改,我設法讓我的自定義字段也在那裏:

$content = ''; 
foreach ($items as $item) { 
    $single_comment = comment_load($item['cid']); 
    $custom_field = field_attach_view('comment', $single_comment, 'field_jf_comment'); 
    $to_render = array(
    '#theme' => 'comment', 
    '#comment' => $single_comment, 
    '#node' => node_load($item['nid']), 
    'field_jf_comment' => $custom_field 
    ); 

    $content .= render($to_render); 
} 

$block['content'] = $content; 

現在我唯一缺少的是每個評論的鏈接。我唯一使用的是回覆評論。任何人都知道如何讓它顯示出來?

回答

0

theme()調用可能會中斷,因爲您使用的是Drupal 7,但試圖通過Drupal 6樣式傳遞參數。如果你看看theme_comment() documentation,你可以看到它需要一個$variables參數,它應該是一個數組。試試這個:

$content = ''; 
foreach ($items as $item) { 
    $to_render = array(
    '#theme' => 'comment', 
    '#comment' => comment_load($item['cid']) 
); 

    $content .= render($to_render); 
} 

$block['content'] = $content; 
+0

感謝您的快速回復。我試了一下,但不幸的是它不工作。如果我完全按照你的建議來做,它會打破整個頁面,並給我這些錯誤: – ghost79 2012-01-04 21:43:42

+0

注意:未定義的變量:include()中的底部(../all/themes/zen/templates/maintenance-page.tpl的第85行.PHP)。 注意:未定義索引:template_preprocess_comment()中的#comment(../modules/comment/comment.module的第2263行)。 注意:未定義索引:template_preprocess_comment()中的#node(../modules/comment/comment.module的第2264行)。 注意:試着在template_preprocess_comment()中獲取非對象的屬性(../comment/comment.module的第2268行)。 警告:date_timezone_set()期望參數1爲DateTime,在format_date()中給出的布爾值(位於../includes/common.inc的第1909行)。 – ghost79 2012-01-04 21:49:58

+0

警告:date_format()期望參數1爲DateTime,在format_date()(在../includes/common.inc的第1919行)中給出的布爾值。 注意:試圖在template_preprocess_comment()(../modules/comment/comment.module的第2269行)中獲取非對象的屬性。 EntityMalformedException:在註釋類型的實體上缺少bundle屬性。在entity_extract_ids()(../includes/common.inc的第7409行)中。 – ghost79 2012-01-04 21:50:17

0

新的Drupal 7 theme()語法需要一個數組作爲其第二個參數。這個數組在調用模板文件之前會被解壓縮,所以每個鍵都會變成一個新的php變量。

例如array('comment' => $mycomment)會在您的模板中爲您提供一個$comment變量。

希望這可以幫助。