Drupal FAPI 表单调​​用回调两次

发布于 2024-10-25 05:25:56 字数 1266 浏览 1 评论 0原文

第一篇关于堆栈溢出的文章...所以对我宽容一些!

对于简单表单提交的 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. 有没有办法识别第一个回调?

预先感谢大家。

First post on stack overflow... so go easy on me!

There doesn't seem to be a suitable solution to the Drupal FAPI multiple callback issue for simple form submissions.

THE PROBLEM: My form, when submitted, adds two entries to the respective database table. Given that there is only one call to add it to the database, I feel it's safe to assume that the query is run twice (hence the dual entries).

The following code may help to provide a basis for a solution. Oh, it's Drupal 7 too, so documentation is still very much D6 centric.

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.'));
}

... and my form function contains a submit button:

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

I guess the questions I need answered are:

  1. Why is there a double callback in the first place?
  2. Is there a way to identify the first callback?

Thanks in advance to all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不寐倦长更 2024-11-01 05:26:03

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

To find out where the second call is coming from, the easiest way is to install devel.module and use ddebug_backtrace() in your submit callback. You might need to disable the HTTP redirecto to see it, too (exit()).

But more importantly, use the API, Luke!

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

This is how your insert query should look like, what you are doing is insecure!

And for SELECT, use db_query() with named placeholders:

<?php
$result = db_query('SELECT * FROM {sidebar_element} WHERE title = :title', array(':title' => $something));
?>
不醒的梦 2024-11-01 05:26:02
  $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);
  $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save')
  );
  $form['#submit'] = array('my_form_submit');

And replace

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

with

// 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);
一腔孤↑勇 2024-11-01 05:26:02

对于 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 () 代码

For 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'),
  );

As example read node_form() code

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文