【问题标题】:Drupal FAPI form calls callback twiceDrupal FAPI 表单调​​用回调两次
【发布时间】:2011-03-22 00:55:43
【问题描述】:

关于堆栈溢出的第一篇文章......所以放轻松!

对于简单表单提交的 Drupal FAPI 多重回调问题似乎没有合适的解决方案。

问题:我的表单在提交时向各自的数据库表中添加了两个条目。鉴于只有一次调用将其添加到数据库中,我认为可以安全地假设查询运行两次(因此有两个条目)。

以下代码可能有助于为解决方案提供基础。哦,它也是 Drupal 7,所以文档仍然非常以 D6 为中心。

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. 有没有办法识别第一个回调?

在此先感谢大家。

【问题讨论】:

    标签: drupal drupal-modules drupal-7 drupal-fapi


    【解决方案1】:
      $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 = sprintf("INSERT INTO sidebar_element(title, image_url, content) 
          VALUES ('%s', '%s', '%s');", $se_title, $se_image, $se_content);
    
      db_query($query);
    

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

    【讨论】:

      【解决方案2】:

      对于 Drupal 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'),
        );
      

      例如阅读node_form()代码

      【讨论】:

        【解决方案3】:

        要找出第二个调用的来源,最简单的方法是安装 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));
        ?>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-05-21
          • 1970-01-01
          • 2022-10-18
          • 1970-01-01
          • 2015-04-23
          • 1970-01-01
          • 2023-04-02
          相关资源
          最近更新 更多