Using webfom 3.x to reserve items/nodes in Drupal 7

Goal: Create a items content type and use webform to reserve those items.

Modules required:

Libraries API
Webform 3.x
Webform Ajax
Colorbox Node

and other modules required by above mentioned modules

Lets start by creating a content type "Items" and add "Item status" List(text) type field.

Add options to the "Item status" and make the field required and "0|Free" as the default value as shown in the images below

Now let's create some nodes(items) of type "Items" and create a view to list them

and add node id, Item Status and Custom text fields to the view


And the view should look something like this

Now let's create a webform which will be used to reserve the items.

and add couple of webform component with "Item id" component which will be mandatory, numeric/integer.

add %get[item_id] as the default value and also select the "disabled" checkbox

and save the webform.

Now lets go to the views and edit "Custom text" field to add link to this webform.

 <a class="colorbox-node" href="node/YOUR WEBFORM NODE ID HERE?item_id=[nid]&amp;width=600&amp;height=600">Reserve</a>


Now let's go the view and click the link, the webform should open in colorbox

We can see that the node id in the Item Id field in the webform. Submitting the Form will not do anything special yet. We need to write a small custom module to manipulate the "Item status" field once the form is submitted. So let's do that now.

I am using "reservable_item_chang_status" as the module name, it can be anything you like though.Create and reservable_item_chang_status.module files and start to fill them up.

This is what i put in file

name = Reservable item change status
description = Changes the status of reservable items when the webform is submitted
core = 7.x

 In our module file, we will now use hook_webform_submission_insert to see the data submitted by the webform first.


function reservable_item_change_status_webform_submission_insert($node, $submission) {
	if($node->nid == YOUR WEBFORM NODE ID){   //webform id used to reserve items

Create "custom" directory inside sites/all/modules and drop the module to the custom folder. Let's enable the module after that

Now let's go to the views and submit a form and see what values we get.

I submitted the webform with the Item id = 3, and i can see that the data[3]['value'][0] holds that value. Now we can use this info to go further.

Let's replace the module code with the one given below

function reservable_item_change_status_webform_submission_insert($node, $submission) {
  if($node->nid == YOUR WEBFORM NODE ID){ //webform id used to reserve items
    $nodeID = $submission->data[3]['value']['0'];
    $reservableItem = node_load($nodeID);
    if($reservableItem ->type == 'items'){
      $reservableItem->field_item_status['und'][0]['value'] = 1;

 Now let's submit form again with the same node id

 And the Item status field changes


Now let's try to make the form so that if the item is reserved, one cannot submit the form (validation). But before this let's enable ajax in our webform (Your webform -> edit-> Webform tab -> Form settings)

and save configuration.

function reservable_item_change_status_form_alter(&$form,&$form_state,&$form_id){
  if($form['#form_id'] == "webform_client_form_YOUR WEBFORM ID HERE"){  //You can find this value by using dpm($form_id) as in the picture above
    $form['#validate'][] = 'reservable_item_change_status_validate';

function reservable_item_change_status_validate(&$form, &$form_state) {
  if($form['submitted']['item_id']['#default_value'] != NULL){ 
    $nodeID = $form['submitted']['item_id']['#default_value'];
        if($node->field_item_status['und'][0]['value'] == 1){
          form_set_error('', t('This item is already reserved!'));