0

I have created a basic page with two buttons, "Yes" and "No". When the "Yes" button is clicked, it is directed to the webform.

Aim: When the webform appears, it displays a value from the table into a field.

Database

  • Table name: Room
  • Field: Room
  • Data: Training Room

The Basic Page

Basic page appearance

The Webform

The Lettable Booking Form

I created a module for this functionality:

test_presave.info

name = "Test presave"

description = "Retrieving data from table"

core = 7.x

package = Test

test_presave.module

/*

*Implements hook_node_presave()

*/

function test_presave_node_presave($node)

{

    $accessnode=$node->nid;

    if($accessnode==20)//Lettable Room Booking webform

    {

        $result=$db_query("SELECT Room FROM {Room} WHERE Room =:ln",array(':ln'=>'Training Room'))->fetchCol();

        $node->webform['components'][1]['value']=$result;

    }

}   

Am I using the correct hook?

Do I have to use the node api or the webform api?

Can you help me with this?

0

Rather do this:

Alter the button link to include a parameter:

<a href="./lettable-room-booking?captured=true">Yes</a>

In hook_form_alter, check for the parameter and set the default value(s):

function test_presave_form_alter(&$form, &$form_state, $form_id) {
    // dpm($form_id); // uncomment to get $form_id
    // dpm($form); // uncomment to get $form
    if ($form_id == 'webform_client_form_2') { // use dpm($form_id) to get your form id
        $param = drupal_get_query_parameters();
        if (isset($param['captured']) && $param['captured'] == "true")
        {
            $result = ...
            if ($result) {
                $form['submitted']['room']['#default_value'] = $result; // use dpm($form) to get your room field
            }
        }
    }
}
  • Thanks for your answer. What is 'room'? Is it the name or the form_key? – Joey May 5 '16 at 9:31
  • It is the form field name that webform gave my room field. Yours might be different. Use dpm($form['submitted']) to inspect – J. Reynolds May 5 '16 at 9:36
  • PS. to get dmp() you need Devel installed – J. Reynolds May 5 '16 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.