如何以及在何处编写Webform提交钩子? [英] How and where to write Webform submit hook?

查看:89
本文介绍了如何以及在何处编写Webform提交钩子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Drupal(7)的新手,因此在以下情况下需要一些帮助。

I am new to Drupal(7) and hence need some help for following situations.

我创建了一个Webform(我也创建了另一个Webform),而现在要插入默认的webform_submitted_data表中,我希望此webfrom插入到myTable中。从我的发现中,我需要为此写一个钩子。实际上,我对于编写此钩子的方式感到困惑。我有以下问题。

I have created one Webform(I have other webform too) and now instead of inserting in default webform_submitted_data table, I want for this webfrom to insert into myTable. From what I found, I need to write a hook for this. Actually I am getting confused for way to write this hook. I have below questions.


  1. 在哪里写这个钩子(在哪个文件中)。

  2. 如何

请帮忙,让我知道您是否需要更多信息。

Please help and let me know if you need any more information for this.

推荐答案

首先,请务必确保在开始扭曲Drupal的手臂以使其工作原理与预期的不同之前。重新路由Webform的数据可能会干扰Webform的工作方式,这以后可能会给您带来麻烦。它可能仍然希望将数据保存在其自己的簿记表中,但是如果您覆盖其行为,以后将无法在其中找到它。

First, be very sure before you start twisting Drupal's arm into making things work differently then they are supposed to. Rerouting the data for a Webform could potentially provide hiccups in how Webform works and this could bite you later. It may still expect the data to be saved in its own bookkeeping tables, but fail to find it there later if you overwrite its behavior.

话虽如此,如果您愿意要更改其他模块(例如Webform)的行为,您将必须编写自己的微型模块。这些钩子中的一些也可以通过模板层(使用模板 template.php 文件)受到影响,但是在我看来,这是更改此类行为的错误位置

That being said, if you wish to alter the behavior of other modules, such as Webform, you will have to write your own, tiny module. Some of these hooks can also be influenced via the templating layer (using your templates template.php file), but this is the wrong place to alter this kind of behavior in my opinion.

Drupal 7模块基本上由至少两个文件组成,分别是 *。info 文件和 *。module 文件。前者包含Drupal对模块进行分类并计算可能的依赖关系所需的一些元数据。后者包含实际的PHP代码。

A Drupal 7 module is basically comprised out of a minimal of two files, a *.info file and a *.module file. The former contains some meta data necessary for Drupal to categorize your module and calculate possible dependencies. The latter contains the actual PHP code.

这些文件必须保存在目录中(名称最好与您命名 info的名称相同) module 文件。为了让Drupal找到您的模块,您可以将其放在 sites / all / modules 下。

These files have to be saved in a directory with (preferably) the same name as you named your info and module file. For Drupal to find your module, you can place it under sites/all/modules.

例如,您将模块命名为 changemyform ,这些是所需的最少文件:

If, for example, you name your module changemyform, these are the minimal required files:


  • changemyform.info

  • changemyform.module

两者都应位于:sites / all / modules / changemyform。

And both should reside in: sites/all/modules/changemyform.

我建议您查看 Drupal的开发人员手册有关编写模块的更详细说明,包括许可,单元测试等。但是对于此示例,上述两个文件即可。

I suggest you check Drupal's developer's manual for a more detailed explanation about writing modules, including licensing, unit testing, ... . But for this example, the mentioned two files will do.

在您的 info 文件中,您至少必须编写模块的名称,简短描述,适用于哪个核心版本以及具有哪些依赖项。这样的例子足以满足我们的示例:

In your info file you have to at least write the name for the module, a small description, for which core version it is applicable and which dependencies it has. Something like this would suffice for our example:

name = Change my form
description = Changes the submission behavior of my form.
core = 7.x
dependencies[] = webform

接下来我们应该编写 module 文件本身的逻辑。拦截任何表单提交(包括Web表单)的一般方法是:

Next we should write the logic for the module file itself. The general hook for intercepting any form submission (including a Webform) is:

function mymodule_form_alter( &$form, &$form_state,$form_id ){
  ...
}

使用此钩子,顾名思义,您可以更改 all 用Drupal渲染的表单。不仅是提交处理程序,还包括添加/删除字段,添加标记等。将 mymodule 替换为模块的实际名称,在我们的示例中为 changemyform 。接下来,您需要将其范围缩小到仅影响所需的形式:

With this hook you can, as the name suggests, alter all the forms rendered with Drupal. Not only the submission handler, but add/remove fields, add markup, ... . Replace mymodule with the actual name of your module, in our example case changemyform. Next you need to scope it down to only effect your desired form:

function changemyform_form_alter( &$form, &$form_state,$form_id ){
  if ($form_id == 'my_desired_webform_form_id') {
    $form['#submit'][] = 'changemyform_submit_handler';
  }
}

请注意,我现在替换 mymodule changemyform 。您还可以看到,我已经在表单的Submit属性中添加了自定义处理程序。您将不得不将此处理程序编写为一个函数,然后将包含所需的所有逻辑。因此,整个 module 文件现在变为(减去<?php ?> 标记):

Notice that I now replace mymodule with changemyform. As you can also see I have added a custom handler to the form's submit property. You will have to write this handler as a function which then will contain all the logic you desire. Thus the total module file now becomes (minus the <?php ?> tags):

function changemyform_form_alter( &$form, &$form_state,$form_id ){
  if ($form_id == 'my_desired_webform_form_id') {
    $form['#submit'][] = 'changemyform_submit_handler';
  }
}

function changemyform_submit_handler($form, &$form_state) {
  ... your submission logic ...
}

现在,您可以编写所需的所有逻辑,以捕获提交时的数据并随心所欲。

Now you are ready to write all the logic you need to capture the data on submit and do as you please.

由于这是一个模块,因此,您当然应该在管理模块概述屏幕中启用它才能起作用。

Since this is a module, you should, of course, enable it in your administration modules overview screen for it to function.

在编写自己的模块时,也要(作为 nitpick)用文档标题来修饰每个函数,这些文档标题描述了每个功能做什么以及每个参数可以保存什么。即使是微小的琐碎功能。

Also (as a nitpick) when writing your own modules, do decorate each function with documentation headers that describe what each function does and what each parameter could hold. Even for tiny, trivial functions.

这篇关于如何以及在何处编写Webform提交钩子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆