10
二/09
3

定制drupal评论



drupal的评论系统比较难定制,如果我们想获得除了默认评论模板变量之外的内容,比如node的相关信息,我们就要使用高级模板功能,template.php文件。下面的例子使我们可以在评论模板(comment.tpl.php)中直接调用$node_type来判断节点的类型。

function _phptemplate_variables($hook, $vars=array()) {
  switch ($hook) {
    case ‘comment’:
      global $user;
      // We load the node object to which the current comment is attached.
      $node = node_load($vars['comment']->nid);
      $vars['node_type'] = $node->type; // 这行往评论模板中赋值,类似smarty的assign功能。
      break;
  }
  return $vars;
}

这样在评论模板中我们就可以判断:

if (‘sometype’ == $node_type) {

  // do sth…

}

至于评论表单的修改,则可以使用form_alter这个钩子,比如我们的模块叫test,

function test_form_alter($form_id, &$form) {
  if (‘comment_form’ == $form_id) {

    // do sth.

  }

}


相关文章

关键字:
评论 (3) Trackbacks (0)
  1. follow.Ken
    10:37 on 二月 20th, 2009

    这个form_alter的钩子一直不知道怎么使用啊,用在什么时候,参数都是什么?

  2. terrysco
    12:15 on 二月 20th, 2009

    表单渲染前对表单结构进行修改,参数是表单的结构关联数组,为地址参数。可以对其值进行修改

  3. purifly
    16:08 on 四月 7th, 2009

    明白了,呵呵

发表评论

No trackbacks yet.