【问题标题】:Add comments to Custom node module向自定义节点模块添加注释
【发布时间】:2012-10-10 11:08:36
【问题描述】:

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

// 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;

上面的代码将 cmets 表单放到我的节点页面中。

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

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

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

我希望发生的是:

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

我尝试过的事情

  • 添加重定向

    $form['#redirect'] = "/success-stories/".$node_good_practice->good_practice_name."/".$node_good_practice->nid;
    

    它没有改变任何东西。

  • 改变动作

    $form['#action'] = "/success-stories/".$node_good_practice->good_practice_name."/".$node_good_practice->nid;
    

    它将我重定向到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));
    

    表单正在显示,但没有重定向;它被发送到comment/reply/node_id

【问题讨论】:

  • 您说代码有效,然后描述它无法保存您的评论。您能否更清楚地说明哪些有效,哪些无效?
  • 我编辑了问题,这更清楚了吗?
  • 除非您不引用特定的 PHP 版本,例如 PHP-5.3(我认为这里不是这种情况),否则只需标记为 PHP,而不是 PHP5。谢谢!
  • 可能与this有关
  • @Clive - 感谢链接。我尝试了链接,但没有帮助。可能是因为该链接描述了模块中自行创建的表单,而这不适用于我正在使用的核心 cmets 表单。

标签: drupal drupal-7


【解决方案1】:

由于您使用的是自定义模块,因此您可以针对您的特定情况使用form_alter 挂钩更改comment_form。您可以将表单设置为仅使用您的模块提交功能。然后在自定义提交函数中,将评论提交到评论模块保存(调用comment_form_submit函数),然后自己重​​定向回节点。

类似这样的事情:

<?php
    function mymodule_form_alter(&$form,&$form_state,$form_id){
        if ($form_id == 'comment_form' && isset($form['#node']) && ($form['#node']->type == 'mynodetype')){
            $form['#submit'] = array('mymodule_comment_form_submit');
        }
    }

    function mymodule_comment_form_submit($form,&$form_state){
        module_load_include('module','comment');
        comment_form_submit($form,$form_state);
        $url = drupal_get_path_alias('node/'.$form['#node']->nid);
        header('Location: '.$url, TRUE);
        drupal_exit($url);
    }

在您的模板文件中,仍然按照您的方式构建评论表单:

$info->nid = $node_good_practice->nid;
$comment['build_info']['args'][0] = $info;
$output .= render(drupal_build_form('comment_form', $comment));

这个解决方案可能看起来有点老套,但它确实有效。

【讨论】:

  • 不幸的是它仍在重定向到 /comment/reply/ 但你的想法似乎是正确的。我将进一步排除故障,看看哪里出了问题。我会尽快通知您。
  • 很有趣,因为 drupal_goto 应该将用户带回节点页面。我想知道是否在导致重定向的 comment_form_submit(或者更确切地说是它调用的函数)中触发了一个函数。
  • 如果 URL 中有 destination 查询参数,@Mike drupal_goto() 将忽略您传递给它的内容。我很确定 Drupal 在评论回复表单链接中添加了这样一个参数,所以这可能是问题
  • @Clive 你是对的,只是查看了 drupal_goto 的文档,如果作为 get 变量存在,它将使用目标路径。 Jurgo,我已经更新了我的代码,它可以使用 php header() 和 drupal_exit(),这样你就不用担心目的地了。
【解决方案2】:

我猜这就是答案?

$comment->nid = $row->nid;
$form = drupal_get_form('comment_form', $comment);
$form['#redirect'] = 'CHANGE_VIEWSPAGE_HERE?page=' . (int)$_GET['page'];
print render($form);

抱歉,我自己无法尝试。我在https://drupal.stackexchange.com/questions/21692/d7-comment-form-doesnt-submit找到它

【讨论】:

  • 感谢您的回答。然而重定向选项并没有对结果做出任何改变。
【解决方案3】:

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

因此,Mike 的解决方案仅在您取消设置 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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-26
    • 2012-02-02
    • 1970-01-01
    • 1970-01-01
    • 2017-11-19
    • 1970-01-01
    • 2011-07-15
    相关资源
    最近更新 更多