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

查看:23
本文介绍了如何以及在哪里编写 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.

这些文件必须保存在(最好)与您命名的 infomodule 文件同名的目录中.为了让 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 文件本身编写逻辑.拦截任何表单提交(包括 Webform)的通用钩子是:

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 ){
  ...
}

使用这个钩子,顾名思义,您可以更改所有由 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.您还可以看到,我在表单的提交属性中添加了一个自定义处理程序.您必须将此处理程序编写为一个函数,该函数将包含您想要的所有逻辑.因此整个 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.

另外(作为一个挑剔的人)在编写自己的模块时,请使用文档标题来装饰每个函数,这些文档标题描述了每个函数的作用以及每个参数可以包含的内容.即使是微不足道的函数.

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天全站免登陆