I have a node page where I need to create a reply form via ajax for commenting. Commenting in my system is also a kind of node.
我有一个节点页面,我需要通过ajax创建一个回复表单进行评论。在我的系统中评论也是一种节点。
When my page first loads it's presented with all comments done and also only one textbox & submit button which is used to add a new comment node. When I click submit button that comment is added via ajax.
当我的页面首次加载时,它会显示所有已完成的注释,并且只有一个文本框和提交按钮,用于添加新的注释节点。当我点击提交按钮时,评论是通过ajax添加的。
Below each comments there is reply option. When I click on this reply, an ajax call is made to bring a new text area & reply button which are coming using same node form that is presented in form above. When this new form is presented I fill it up and submit at that time it's not submitted via ajax instead it redirects to destination URL.
每条评论下方都有回复选项。当我点击这个回复时,进行ajax调用以带来一个新的文本区域和回复按钮,它们将使用上面表格中显示的相同节点形式。当这个新表单出现时,我填写并提交,那时它不是通过ajax提交的,而是重定向到目标URL。
Following is my menu & form alter code.
以下是我的菜单和表格更改代码。
function mymodule_menu() {
$items['user_reply_comments/%/%'] = array(
'title' => t('Reply to User Comment'),
'page callback' => 'mymodule_user_reply_comment',
'page arguments' => array(1, 2, 3),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
'delivery callback' => 'ajax_deliver',
'file' => 'includes/mymodule.pages.inc',
);
return $items;
}
function mymodule_form_comment_node_statement_form_alter(&$form, &$form_state, $form_id) {
drupal_add_library('system', 'drupal.form');
drupal_add_library('system', 'drupal.ajax');
drupal_add_js($base_url. '/' .path_to_theme().'/js/script.js', 'file');
$form['#action'] = url('user_reply_comments/'.$id.'/'.arg(2) .'/ajax');
$form['actions']['submit']['#ajax'] = array(
'callback' => 'mymodule_comment_node_statement_form_callback',
'wrapper' => 'comment-replies-' . arg(2),
'method' => 'replace',
'effect' => 'fade',
'event' => 'click',
'progress' => array('type'=> 'throbber',
'message' => "<div></div>",
),
'js' => array(
$base_url. '/' .path_to_theme().'/js/script.js',
),
);
}
/**
* Ajax callback for comment_node_statement_form.
*/
function mymodule_comment_node_statement_form_callback($form, &$form_state) {
/*echo '<pre>';
print_r($form);
exit;*/
//echo ' 1 :' . arg(1) . ' 2:' .arg(2); exit; // sub reply dows not came here
$message_empty = drupal_get_messages();
if(!empty($message_empty) && isset($message_empty ['error'])){
global $base_url;
$command_add = array();
if(user_is_anonymous()){
$command_add = ajax_command_invoke(NULL, "redirect_user", array($base_url));
}
return array(
'#type' => 'ajax',
'#commands' => array(
ajax_command_before('#content', '<div id="inline-messages" class="messages error">'.$message_empty ['error'] [0].'</div>'),
ajax_command_invoke(NULL, "greyout_reply"),
$command_add
)
);
}
else{
$current_path = TRUE;
$statement_page_reply_form = "";
$pos = strpos($_SERVER['HTTP_REFERER'], "statements");
if($pos !== FALSE){
$current_path = FALSE;
}
// print_r($_SERVER['HTTP_REFERER']);
// print "$$$".$current_path."@@@"; exit;
// Clear the messages (drupal_set_message() doens't work for some reason).
$_SESSION['messages'] = '';
if($current_path){
$html = "<div class='arrow'>arrow</div>";
}
else{
$html = "";
}
$id = $form['#node']->nid;
if (!empty($form['build_info_args_' . $id]['#value'])) {
$form_state = array(
'build_info' => array('args' => $form['build_info_args_' . $id]['#value']),
'values' => array(),
) + form_state_defaults();
}
$form_state['rebuild'] = TRUE;
//$comment_form = drupal_rebuild_form('comment_node_statement_form', $form_state, $form);
$comment_form = drupal_rebuild_form('comment_node_statement_form', $form_state, $form);
$node_comments = views_embed_view('statement_replies', 'block', $id);
if($form['current_url']['#value'] == "dashboard"){
$comment_form['current_url'] = array('#type' => 'hidden', '#value' => "dashboard", '#name' => 'current_url');
$html .= '<div class="dashboard-statements-comments">';
}
if($current_path){
$html .= '<div class="statement-comment-reply-box">'.drupal_render($comment_form).'</div>';
}
else{
$statement_page_reply_form = '<div class="arrow">arrow</div><div class="statement-comment-reply-box">'. drupal_render($comment_form).'</div>';
$num_comments = db_query("SELECT COUNT(cid) AS count FROM {comment} WHERE nid =:nid and status = :status",array (":nid"=>$id, ":status" => '1'))->fetchField();
$statement_page_reply_form .= '<span class="statement_total_replies">'.$num_comments. ' Replies</span>';
$statement_page_reply_form .= '<span class="collapse_reply">COLLAPSE</span>';
}
$html .= '<div class="statement-comment-reply-feed">'.$node_comments.'</div>';
if($form['current_url']['#value'] == "dashboard"){
$html .= '</div>';
}
if($current_path){
return array(
'#type' => 'ajax',
'#commands' => array(
ajax_command_html('#statement-replies-' . $id, $html),
ajax_command_remove('#inline-messages'),
ajax_command_before('#content', '<div id="inline-messages" class="messages status"><h2 class="element-invisible">Status message</h2>Reply posted.</div>'),
)
);
}
else{
return array(
'#type' => 'ajax',
'#commands' => array(
ajax_command_html('#statement-replies-' . $id, $html),
ajax_command_html('.statement_upper_part .reply_form_statement', $statement_page_reply_form),
ajax_command_remove('#inline-messages'),
ajax_command_before('#content', '<div id="inline-messages" class="messages status"><h2 class="element-invisible">Status message</h2>Reply posted.</div>'),
ajax_command_invoke(NULL, "alter_elements"),
)
);
}
}
}
/**
* This is function in mymodule.pages.inc
* Callback function for 'user_reply_comment/%/%' path.
*
* Returns the popup modal form consists of reply form
*
* @param $entity_id
* Integer representing node ID
* @param $current_cid
* Integer representing comment ID
*/
function mymodule_user_reply_comment($entity_id, $current_cid, $js = FALSE) {
$logged_in = user_is_logged_in();
$node = node_load($entity_id);
if($node) {
if ($node->type == 'statement' || $node->type == "user_comment") {
$replies = "<div class='arrow'>arrow</div>";
$ajax_css = "";
if ($logged_in) {
$edit = array('nid' => $node->nid);
$form = drupal_get_form("comment_node_{$node->type}_form", (object) $edit);
$replies .= "<div class='statement-comment-reply-box'>";
$replies .= drupal_render($form);
$replies .= "</div>";
}
else{
$ajax_css = ajax_command_invoke(NULL, "views_row_hide_border");
}
$replies .= "<div class='statement-comment-reply-feed'>";
$replies .= "</div>";
return array(
'#type' => 'ajax',
'#commands' => array(
ajax_command_html('#comment-replies-' . $current_cid, $replies),
ajax_command_invoke(NULL, "show_statement_replies", array('#comment-replies-' . $current_cid)),
ajax_command_invoke(NULL, "toggle_reply_links" , array($current_cid)),
ajax_command_invoke(NULL, "mymodule_user_reply_comment_reply" , array($current_cid, $edit)),
$ajax_css
));
}
}
}
/*
Following is there in script.js file
*/
(function($) {
$.fn.mymodule_user_reply_comment_reply = function(cid, nid) {
var form_class = '.comment-form-' + nid + '--' + cid;
var button = form_class + ' input.form-submit';
console.log('reply thread click.' + cid);
$(button).bind('click', 'upload_reply_image');
/*{
console.log($(this).attr('id'));
return false;
e.preventDefault();
e.stopPropogation();
//Drupal.attachBehaviors($(form_class));
return false;
});//*/
console.log('reply thread out.' + cid);
};
})(jQuery);
/* To toggle between Reply expand/collapse mode in statement/Comment box and their reply box - Start */
(function($) {
$.fn.toggle_reply_links = function(cid) {
jQuery('#close-sub-comments-' + cid).parent().find('a').toggle();
};
})(jQuery);
(function($) {
$.fn.show_statement_replies = function(id) {
jQuery(id).slideDown();
};
})(jQuery);
Thanks in advance.
提前致谢。
1 个解决方案
#1
0
I think you have wrong path in your code. You need to change this in hook_menu:
我认为你的代码中有错误的路径。你需要在hook_menu中更改它:
$items['user_reply_comments/%/%'] = array(
With this:
$items['user_reply_comments/%/%/ajax'] = array(
At least there's something that doesn't match in my opinion. Clear your menu cache to see the changes.
至少在我看来有些东西是不匹配的。清除菜单缓存以查看更改。
Hope that helps.
希望有所帮助。
#1
0
I think you have wrong path in your code. You need to change this in hook_menu:
我认为你的代码中有错误的路径。你需要在hook_menu中更改它:
$items['user_reply_comments/%/%'] = array(
With this:
$items['user_reply_comments/%/%/ajax'] = array(
At least there's something that doesn't match in my opinion. Clear your menu cache to see the changes.
至少在我看来有些东西是不匹配的。清除菜单缓存以查看更改。
Hope that helps.
希望有所帮助。