2011-03-22 6 views
0

스택 오버플로의 첫 번째 게시물 ... 너무 쉽게 이동합니다!Drupal FAPI 양식이 콜백을 두 번 호출합니다.

간단한 양식 제출을 위해 Drupal FAPI 다중 콜백 문제에 대한 적절한 해결책이없는 것 같습니다.

문제 : 양식을 제출할 때 해당 데이터베이스 테이블에 두 개의 항목이 추가됩니다. 데이터베이스에 추가하는 호출이 하나 뿐이므로 쿼리가 두 번 실행된다고 가정하는 것이 안전하다고 느낍니다 (따라서 이중 항목).

다음 코드는 솔루션의 기반을 제공하는 데 도움이 될 수 있습니다. 오, 드루팔 (Drupal) 7이기 때문에 문서화가 여전히 매우 중심적입니다.

function mymodule_sidebar_form_add_submit(&$form, &$form_state) { 

    $form_values = $form_state['values']; 

    $se_title = check_plain(trim($form_values['title'])); 
    $se_link = url(trim($form_values['link'])); 
    $se_content = check_plain(trim($form_values['content'])); 
    $se_image = isset($form_values['image']) ? $form_values['image'] : ''; 

    // The multi-line part below is actually a single line the real code 
    $query = sprintf("INSERT INTO sidebar_element(title, image_url, content) 
     VALUES ('%s', '%s', '%s');", $se_title, $se_image, $se_content); 

    db_query($query); 
    drupal_set_message(t('Sidebar Element has been added successfully.')); 
} 

... 그리고 내 양식 함수가 제출 버튼이 포함되어

$form['submit'] = array(
     '#value' => t('Add Sidebar'), 
     '#type' => 'submit', 
     '#title' => t('Add Sidebar'), 
     '#submit' => array('mymodule_sidebar_form_add_submit'), 
    ); 

내가 대답해야 할 질문을 추측은 다음과 같습니다

  1. 왜 처음에 이중 콜백이 장소?
  2. 첫 번째 콜백을 식별하는 방법이 있습니까?

미리 감사드립니다.

답변

0
$form['submit'] = array(
     '#type' => 'submit', 
     '#value' => t('Save') 
); 
    $form['#submit'] = array('my_form_submit'); 

예로서

// The multi-line part below is actually a single line the real code 
    $query = "INSERT INTO {sidebar_element} (title, image_url, content) 
     VALUES ('%s', '%s', '%s')"; 

    db_query($query, $se_title, $se_image, $se_content); 
+0

죄송합니다 ... 드루팔 7 죄송합니다 – dobeerman

0

드루팔 7의

// Add the buttons. 
    $form['actions'] = array('#type' => 'actions'); 

    $form['actions']['submit'] = array(
    '#type' => 'submit', 
    '#access' => my_func(), 
    '#value' => t('Save'), 
    '#weight' => 100, 
    '#submit' => array('my_form_submit'), 
); 

판독

// The multi-line part below is actually a single line the real code 
    $query = sprintf("INSERT INTO sidebar_element(title, image_url, content) 
     VALUES ('%s', '%s', '%s');", $se_title, $se_image, $se_content); 

    db_query($query); 

교체 node_form() 코드

0

두 번째 호출의 출처를 확인하려면 가장 쉬운 방법은 devel.module을 설치하고 제출 콜백에 ddebug_backtrace()를 사용하는 것입니다. HTTP 리디렉션을 사용하지 않도록 설정해야 할 수도 있습니다 (exit()).

더 중요한 것은 API, Luke!

<?php 
db_insert('sidebar_element') 
    ->fields(array(
    'title' => $se_title, 
    'image_url' => $se_image, 
    'content' => $se_content, 
)) 
    ->execute(): 
?> 

이렇게하면 삽입 쿼리가 안전하지 않은 것처럼 보입니다.

그리고 SELECT에 대한

라는 자리에 db_query()를 사용

<?php 
$result = db_query('SELECT * FROM {sidebar_element} WHERE title = :title', array(':title' => $something)); 
?> 
관련 문제