添加注释到自定义节点模块

Jur*_*rgo 10 drupal drupal-7

在我的自定义模块中,我想添加注释功能.我尝试了几件事,但到目前为止还没有锻炼.

// render comments form
$output .= theme('my_module_front_page'); 
$comment = new stdClass;
$comment->nid = $node_good_practice->nid;
$output .= render(drupal_get_form('comment_form', $comment));
return $output;
Run Code Online (Sandbox Code Playgroud)

上面的代码将注释表单放到我的节点页面.

但是当我填写评论表并提交时,它会将我重定向到这个页面:comment/reply/node id然后我必须重新填写我的评论并且评论不会​​被保存.

我想提交并保持在同一页面而不是重定向.提交后必须保存评论.

现在,评论表单出现在我的节点页面(自定义模块模板)上.我输入评论并点击"保存".我被发送到/comment/reply/<node_id>,但所有评论字段都是空的.评论也没有保存.

我想要发生的是:

  • 在节点页面上有一个评论表单
  • 输入评论
  • 点击"保存"
  • Drupal保存评论,并将我重定向回我正在查看的节点/页面.

我尝试过的事情

  • 添加重定向

    $form['#redirect'] = "/success-stories/".$node_good_practice->good_practice_name."/".$node_good_practice->nid;
    
    Run Code Online (Sandbox Code Playgroud)

    它没有改变任何东西.

  • 改变行动

    $form['#action'] = "/success-stories/".$node_good_practice->good_practice_name."/".$node_good_practice->nid;
    
    Run Code Online (Sandbox Code Playgroud)

    它将我重定向到 node/node_id/#comment-17

  • 使用 drupal_build_form()

    $info->nid = $node_good_practice->nid;
    $comment['build_info']['args'][0] = $info;
    $comment['redirect'] = "http://www.google.nl";
    $output .= render(drupal_build_form('comment_form', $comment));
    
    Run Code Online (Sandbox Code Playgroud)

    表单正在显示,但不会重定向; 它发送给comment/reply/node_id.

Jur*_*rgo 0

由于某种原因,该问题是由提交评论引起drupal_build_formdrupal_get_form。如果 $_POST 已填充,则 drupal_build_form 和 drupal_get_form 函数将重定向到/node/node_id/#comment-17或 到/comment/reply/<node_id> 所以我在加载表单之前取消设置 SESSION,问题得到解决。

因此,迈克的解决方案仅在您取消设置会话时才有效。但他非常乐于助人。

所以现在我有:

if(isset($_POST['form_id'])) {
  $comment = new stdClass();
  $comment->nid = $good_practice_id; // Node Id the comment will attached to
  $comment->name = $user->name;
  $comment->status = 1;
  $comment->language = "und";
  $comment->subject = $_POST['subject']; 
  $comment->comment_body[$comment->language][0]['value'] = $_POST['comment_body'][$node_good_practice->language][0]['value'];
  $comment->comment_body[$comment->language][0]['format'] = 'filtered_html'; 
  comment_submit($comment);
  comment_save($comment);
  unset($_POST);
} 

$comment = new stdClass;
$comment->nid = $node_good_practice->nid;
$form = drupal_get_form('comment_form', $node_good_practice);
$form['#action'] = url('success-stories/'.$node_good_practice->good_practice_name.'/'. $comment->nid);

$output .= theme('good_practices_front_detail_page', array('oGoodPractice' => $oGoodPractice, 'aGoodPractices' => $aGoodPractices, 'aComments' => $aComments, 'oSectors' => $oSectors, 'oCountries' => $oCountries, 'links' => $aLinks));
$output .= render($form);
$output .= theme('pager', array('tags'=>array()));

return $output;
Run Code Online (Sandbox Code Playgroud)