2011-12-21 5 views
1

내가 원하는 것은 다음과 같습니다.보기가있는 페이지에 양식을 렌더링하고 싶습니다. 이보기에는 '메모'(= CT 메모) 목록이 있습니다. 양식을 채울 때 메모가 저장되고 양식이 지워지고 새 메모가 목록에 추가됩니다. 페이지 새로 고침없이 모두.drupal 아약스 양식

난 모듈 new_note를 생성하고,이 기능 addes :

function new_note_form($nodeid = NULL) { 
     ctools_include('ajax'); 
     // drupal_add_js(drupal_get_path('module', 'custom_forms') . '/js/note_form.js'); 
     //dpm($nodeid); 
     module_load_include('inc', 'node', 'node.pages'); 
     $form = node_add('note'); 
     $form['field_note_reference']['und']['#value'] = '2'; 
     $form['field_note_reference']['und']['#validated'] = 'TRUE'; 
     $form['field_note_reference']['#attributes']['class'][] = "hidden"; 
     $form['submit'] = array(
      '#type' => 'submit', 
      '#value' => 'Submit', 
      '#executes_submit_callback' => FALSE, 
      '#ajax' => array(
       'callback' => 'ajax_note', 
       'wrapper' => 'status', 
      ), 
     ); 
    $output= drupal_render($form); 
     dpm($form); 
     print $output; 
    } 

function ajax_note(&$form, &$form_state) { 
    return 'test'; 
} 

I 음표 목록 위에 렌더링되어 디스플레이 실내 블록 필드,이 기능을 사용한다. 여태까지는 그런대로 잘됐다.

유일한 문제는 양식을 제출할 때 ajax가 호출되지 않고 정상적인 제출이 완료된다는 것입니다.

아무도 도와 줄 수 있습니까?

@ 편집.

clive가 제안한 코드를 변경 한 후 코드를 변경하고 아약스를 작동 시켰습니다.

function new_notes_form($nodeid = NULL) { 
    global $user; 

    $node = (object) array(
       'uid' => $user->uid, 
       'name' => (isset($user->name) ? $user->name : ''), 
       'type' => 'note', 
       'language' => LANGUAGE_NONE, 
    ); 
    $form_state = array(); 
    $form_state['build_info']['args'] = array($node); 
    form_load_include($form_state, 'inc', 'node', 'node.pages'); 
    $form = drupal_build_form('note_node_form', $form_state); 
    $form['field_note_reference']['und']['#value'] = '2'; 
    $form['field_note_reference']['#attributes']['class'][] = "hidden"; 
    $form['submit'] = array(
     '#type' => 'button', 
     '#value' => 'Submit', 
     '#limit_validation_errors' => array(), 
     '#ajax' => array(
      'callback' => 'ajax_note_replace', 
      'wrapper' => 'status', 
     ), 
    ); 
    return $form; 
} 

function ajax_note_replace(&$form, &$form_state) { 
    dpm("test"); 
    dpm($form); 
    $output = '<h1>' . t('Hello World') . '</h1>'; 
    // $node = node_load('6'); 
    // $output .= drupal_render(node_view($node, $style = 'teaser', $options = array())); 

    ctools_include('ajax'); 
    $commands = array(); 
    $commands[] = ajax_command_prepend(".view-content", $output); 
    print ajax_render($commands); // this function exits. 
    exit; 
} 

@Clive, 나머지는 도와 줄 수 있습니까? 콜백에 노드를 저장하고 싶습니다 (유효한 경우?). ajax 콜백에서 node-id는 아직 저장되지 않았기 때문에 null입니까? 노드를 확인하고 저장할 수있는 방법, 그렇지 않은 경우 정상적인 양식 항목을 정상적으로 빨간색으로 설정하십시오.

+0

'advanced_form_callback()'에 대한 코드를 게시 할 수 있습니까? – Clive

+0

죄송합니다. 버전이 잘못되어 업데이트되었습니다. – Nealv

답변

5

AJAX 사전 처리가 수행되지 않도록 양식을 만드는 드루팔 (Drupal)의 적절한 방법을 피하고 있기 때문일 수 있습니다. 당신이 drupal_get_form()를 읽어보십시오 당신이해야 할 일이다 차례 drupal_build_form()를 호출에 당신이 그것을 볼 수 있습니다 (이 기능은 드루팔에서 양식을 준비하기 위해 거의 독점적으로 사용) :

function new_note_form($form, &$form_state, $nodeid = NULL) { 
    $form_state['build_info']['args'] = array('note'); 
    $form = drupal_build_form('node_add', $form_state); 

    $form['submit'] = array(
    '#type' => 'button', 
    '#value' => 'Submit', 
    '#limit_validation_errors' => array(), 
    '#ajax' => array(
     'callback' => 'advanced_form_callback', 
     'wrapper' => 'status', 
    ), 
); 

    return $form; 
} 

echo render(drupal_get_form('new_note_form')); 

내가 변경 한 요소가 submit에서 button으로 변경 되었기 때문에 Ajax 처리를 수행하고 싶을 때 #executes_submit_callback을 제거하고 양식 유효성 검사를 중지 할 #limit_validation_errors을 추가했기 때문에 (내가 생각하기에 사용자가 #validated으로 설정하려고 한 것 같습니다. TRUE하지만 잘못된 것 같습니다.)

도움이 되길 바랍니다. 테스트되지 않았으므로 시작할 수있는 좋은 장소를 제공해야합니다.

+0

흠, 알 겠어 : 정의되지 않은 색인 : drupal_retrieve_form()의 node_add – Nealv

+0

아, 죄송합니다. 'module_load_include ('inc ','node ','node.pages '); 당신이 원래의 기능에서했던 것처럼. 또한 AJAX 콜백에서 양식 요소를 반환하거나 [AJAX 함수] (http://api.drupal.org/api/drupal/includes--ajax.inc/group/ajax)를 사용하여 작성된 ajax 명령을 반환해야합니다. – Clive

+0

노드 검증 및 저장을 도와 주시겠습니까? – Nealv