2012-01-04 46 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变量。

希望这可以帮助。