2014-09-17 5 views
0

나는 ajax를 통해 답장 폼을 작성해야하는 노드 페이지가 있습니다. 내 시스템에 주석 달기도 일종의 노드입니다.Drupal 7 ajax form submit not working

내 페이지가 처음로드 될 때 완료된 모든 주석과 새로운 코멘트 노드를 추가하는 데 사용되는 텍스트 상자 & 제출 버튼 하나만 표시됩니다. 제출 버튼을 클릭하면 코멘트가 아약스를 통해 추가됩니다.

각 댓글 아래에는 회신 옵션이 있습니다. 이 회신을 클릭하면 ajax 호출이 생성되어 위 양식에서 제시된 동일한 노드 양식을 사용하여 새 텍스트 영역 & 답장 버튼이 나타납니다. 이 새로운 양식이 제시되면 내가 작성하고 제출할 때 아약스를 통해 제출되지 않고 도착 URL로 리디렉션됩니다.

다음은 내 메뉴 & 양식 변경 코드입니다.

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

미리 감사드립니다.

답변

0

코드에 잘못된 경로가 있다고 생각합니다. 당신은 hook_menu에서 변경할 필요가 :이

$items['user_reply_comments/%/%'] = array(

:

$items['user_reply_comments/%/%/ajax'] = array(

것은 적어도 내 의견과 일치하지 않는 무언가가있다. 변경 사항을 보려면 메뉴 캐시를 지우십시오.

희망이 있습니다.

+0

hook_menu()에서 "ajax"를 사용하고 캐시를 삭제했지만 여전히 작동하지 않았습니다. –