How and where to write Webform submit hook?

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

I created one Webform (I have another webform too) and now instead of inserting into the default webform_submitted_data table, I want this webfrom to insert into myTable. From what I've found, I need to write a hook for this. Actually I am confused to write this hook. I have some questions.

  • Where to write this hook (in which file).
  • How to write this hook for just one web form.

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

+4


source to share


3 answers


First, be very confident before you start twisting Drupal's hand to make things work differently than intended. Data redirection for Webform can potentially provide hiccups to the way Webform works, and that can bite you later. He can still expect the data to be stored in his own ledger tables, but he won't be able to find it there later if you overwrite his behavior.

If you want to change the behavior of other modules like Webform, you have to write your own tiny module. Some of these hooks can also be influenced at the template level (using your template.php

file templates ), but this is the wrong place to change this behavior in my opinion.

A Drupal 7 module basically consists of a minimum of two files, a file *.info

and a file *.module

. The first contains some metadata Drupal needs to categorize your module and calculate possible dependencies. The latter contains the actual PHP code.

These files should be saved in a directory with (preferably) the same name as your file info

and module

. So that Drupal can find your module, you can place it under sites/all/modules

.

If you call your module changemyform, for example, these are the minimum required files:

  • changemyform.info
  • changemyform.module

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

I suggest you check the Drupal Developer's Guide for a more detailed explanation on writing modules, including licensing, unit testing, .... But for this example the two files mentioned will be mentioned.

In your file, info

you should at least write down the name for the module, a little description for which it is used, and what dependencies it has. Something like this would be sufficient for our example:

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

      



Next, we have to write the logic for the file itself module

. A general hook for intercepting any form submission (including a web form):

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

      

With this hook, you can, as the name suggests, modify all forms created with Drupal. Not only the submit handler, but also adding / removing fields, adding markup, .... Replace mymodule with the actual name of your module, in our example case changemyform. Then you need to expand it to only execute the shape you want:

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

      

Note that I am now replacing mymodule

with changemyform

. As you can also see, I've added a custom handler to the submit property. You will have to write this handler as a function, which will then contain all the logic you want. So the resulting file module

now becomes (minus the tags <?php

?>

):

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

      

You are now ready to write all the logic required to capture the data for sending and executing as you please.

Since this is a module, you must, of course, enable it in the Administration Modules Overview screen for it to function.

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

+8


source


The best way is to use hook_webform_submission_insert ()

Example:



function MYMODULE_webform_submission_insert($node, $submission) {
  // Insert a record into a 3rd-party module table when a submission is added.
  db_insert('mymodule_table')
    ->fields(array(
    'nid' => $node->nid, 
    'sid' => $submission->sid, 
    'foo' => 'foo_data',
  ))
    ->execute();
}

      

+4


source


In Drupal 8, the best way to reference the entity_insert hook is

function MODULE_NAME_entity_insert(Drupal\Core\Entity\EntityInterface $entity) {
    if($entity->getEntityTypeId() == 'webform_submission') {
        $result = \Drupal::database()->insert('TABLE_NAME')
            ->fields([
                'nid' => $node->nid, 
                'sid' => $submission->sid, 
                'foo' => 'foo_data'
            ])
            ->execute();
    }
}

      

0


source







All Articles