Posts Tagged Form
Create Drupal form using theme_table() like module list form (Part II)
Posted by admin in Drupal, Open Source, PHP on June 27th, 2009
This post is the second part of Create Drupal form using theme_table() like module list form post. In first part I have showed how we can create Drupal form like we have in Module list pages. In this post, I am showing how we disable some of the checkboxes so user can’t select them like we have for core modules in module list page. We can’t enable/disable core modules in module list.

Drupal Form using theme_table() function (Part II)
Here I am just showing the changes which we require in code of Part I post.
$form['featured'] = array( '#type' => 'checkboxes', '#options' => $options, '#default_value' => $status, ); // Change this to $form['featured'] = array( '#type' => 'checkboxes', '#options' => $options, '#default_value' => $status, '#process' => array( 'expand_checkboxes', 'featured_disable', ), '#disabled_products' => $disabled, );
In above code “$disabled” is an array containing the productid’s of all the products which we want to show as checked and in disable state. The structure of “$disabled” array is similar to “$status” array. We stored this array in “#disabled_products”. We also added “#process” in above code.
“expand_checkboxes” and “featured_disable” are function names which Drupal call when create and render these checkboxes. “expand_checkboxes()” is defined in Drupal core files and we don’t need to define it again.
function featured_disable($form, $edit) { foreach ($form['#disabled_products'] as $key) { $form[$key]['#attributes']['disabled'] = 'disabled'; } return $form; }
“featured_disable()” function is very important here. Drupal call this function when renders these checkboxes. Here we assigned “disabled” attribute to each checkbox which we want as disable.
Create an action confirm form using “confirm_form()” function in Drupal
Posted by admin in Drupal, Open Source on May 24th, 2009

Doctor's List with delete option

Action confirm form in Drupal
We can easily create an action confirm form using confirm_form() function in Drupal. Normally we need this form when user want to delete something and we want user to confirm his/her action. I am showing here an example in which we are listing all the Doctors information with Edit and Delete option. When user click the delete link we will show him the action confirm form with “Delete” and “Cancel” options. If user click the “Delete” button we will delete that Doctor record from database and if user click on “Cancel” we simply show the doctors list again.
Here is the code. I am using the module name “doctor” in this example.
Step 1. First implement hook_menu() to register new path in Drupal.
function doctor_menu() { $items = array(); $items['doctors'] = array( 'title' => t('Doctors'), 'page callback' => 'doctors_list', 'access arguments' => array('access doctor'), 'type' => MENU_CALLBACK, ); $items['doctors/delete/%doctor_user'] = array( 'title' => t('Delete doctor'), 'page callback' => 'drupal_get_form', 'page arguments' => array('doctor_delete_confirm', 2), 'access arguments' => array('access doctor'), 'type' => MENU_CALLBACK, ); return $items; }
Step 2. Implementing the doctors_list() function to display the list of doctors.
function doctors_list() { $header = array(t('Doctor Name'), t('Gender'), t('Phone No.'), t('Status'), t('Action')); $query = "SELECT * FROM {doctor}"; $rs = db_query($query); $row = array(); if ($rs) { while ($data = db_fetch_object($rs)) { $gender = $data->gender == 'M' ? t('Male') : t('Female'); $status = $doctor->status ? t('active') : t('inactive'); $row[] = array(stripslashes($data->firstname) . ' ' . stripslashes($data->lastname), $gender, stripslashes($data->phoneno), $status, "<a href='/doctors/edit/{$data->doctorid}'>" . t('Edit') . "</a> | <a href='/doctors/delete/{$data->doctorid}'>" . t('Delete') . "</a>"); } } $str .= theme_table($header, $row); return $str; }
Step 3. Create a wildcard loader function doctor_user_load() which we used in “doctors/delete/%doctor_user” path. This function checks the given “ID” in path is valid or not. If the given doctor id is not exists in database then “Page not found.” message will display.
function doctor_user_load($doctorid) { $query = "SELECT * FROM {doctor} WHERE doctorid = %d"; $rs = db_query($query, $doctorid); if ($rs) { while ($data = db_fetch_object($rs)) { return $data; } } return FALSE; }
Step 4. Create the doctor_delete_confirm() function. We are using the “confirm_form()” function in this function to show action confirm form.
function doctor_delete_confirm(&$form_state, $doctor) { $form['_doctor'] = array( '#type' => 'value', '#value' => $doctor, ); return confirm_form($form, t('Are you sure you want to delete this doctor?'), isset($_GET['destination']) ? $_GET['destination'] : "doctors", t('This action cannot be undone.'), t('Delete'), t('Cancel')); }
Step 5. Create the Form API submit function for doctor_delete_confirm form.
function doctor_delete_confirm_submit($form, &$form_state) { $form_values = $form_state['values']; if ($form_state['values']['confirm']) { $doctor = $form_state['values']['_doctor']; doctor_delete($form_state['values'], $doctor->doctorid); drupal_set_message(t('Doctor has been deleted successfully.')); } drupal_goto("doctors"); }
Create Drupal form using theme_table() like module list form
Posted by admin in Drupal, Open Source, PHP on March 22nd, 2009
I am showing here an example for Drupal form like you saw in Module list pages.

Drupal Form using theme_table() function
I am considering here an example of Featured Product Management form so we can easily understand it.
Step 1:- Create a menu hook for registering your page path. This registered path will display the featured product management page.
<?php function product_menu() { $items = array(); $items['featured_product_mgmt'] = array( ‘title’ => ‘Manage featured product’, ‘page callback’ => ‘drupal_get_form’, ‘page arguments’ => array(’featured_product_form’), ‘access arguments’ => array(’access product’), ‘type’ => MENU_CALLBACK, ); return $items; } ?>
Step 2:- Create form object for your form. Here the important part is “featured” form element of type “checkboxes”. Checkboxes type is a group of checkbox and format a set of checkboxes. #options is an associative array, where the key is the #return_value of the checkbox and the value is displayed. We will store other information like name, category, discount etc. of product in form array so we can display it later.
<?php function featured_product_form() { $query = “SELECT p.*, UNIX_TIMESTAMP(p.create_date) AS create_date, c.name FROM {product} p LEFT JOIN {category} c ON p.cid = c.cid WHERE p.status = 1 ORDER BY p.product_name”; $rs = db_query($query); $featured_products = featured_product(); $status = array(); if ($rs) { while ($data = db_fetch_object($rs)) { $options[$data->productid] = ”; $form[$data->productid]['name'] = array(’#value’ => stripslashes($data->product_name)); $form[$data->productid]['category'] = array(’#value’ => stripslashes($data->name)); $form[$data->productid]['discount'] = array(’#value’ => $data->discount . ‘%’); $form[$data->productid]['createdon'] = array(’#value’ => date(’m-d-Y’, $data->create_date)); if (in_array($data->productid, $featured_products)) { $status[] = $data->productid; } } } $form['featured'] = array( ‘#type’ => ‘checkboxes’, ‘#options’ => $options, ‘#default_value’ => $status, ); $form['submit'] = array( ‘#type’ => ’submit’, ‘#value’ => t(’Submit’), ); $form['cancel'] = array( ‘#type’ => ‘markup’, ‘#value’ => l(t(’Cancel’), ‘dashboard’), ); $form['#redirect'] = ‘featured_product_mgmt’; return $form; } ?>
Step 3:- Create a form submit function. Any form submitted using the “submit” button will pass to their corresponding function if it is available. We will extract the user input from the form array and update our database accordingly.
<?php function featured_product_form_submit($form_id, $form) { $form_values = $form['values']; $featured = $form_values['featured']; $selected_products = array(); foreach($featured as $key => $value) { if ($value) { $selected_products[] = “(’$value’)”; } } $value_string = @implode(’,', $selected_products); // Delete all previous featured products $query = “DELETE FROM {product_featured}”; db_query($query); // Insert new featured products if (count($selected_products)) { $query = “INSERT INTO {product_featured}(productid) VALUES $value_string”; db_query($query); } drupal_set_message(t(’Featured product list has been updated successfully.’)); } ?>
Step 4:- Register your modules theme implementation using “hook_theme()” function.
<?php function product_theme() { return array( ‘featured_product_form’ => array(‘arguments’ => array(’form’ => NULL),), ); } ?>
Step 5:- Then finally define your theme function which actually format your form layout in list using the $form array. Using “foreach” loop we navigate through each item of $form array and create $rows array which we pass to “theme_table()” function later. Create the header of your table and call “table_theme()” function with $header and $rows parameters. Call the “drupal_render()” function for render the submit and cancel buttons.
<?php function theme_featured_product_form($form) { $rows = array(); foreach (element_children($form) as $key) { $row = array(); if (isset($form[$key]['name'])) { $status = drupal_render($form['featured'][$key]); $row[] = array(’data’ => $status, ‘class’ => ‘checkbox’); $row[] = ‘‘. drupal_render($form[$key]['name']) .’‘; $row[] = array(’data’ => drupal_render($form[$key]['category'])); $row[] = array(’data’ => drupal_render($form[$key]['discount'])); $row[] = array(’data’ => drupal_render($form[$key]['createdon'])); $rows[] = $row; } } // Individual table headers. $header = array(); $header[] = array(’data’ => t(’Featured’), ‘class’ => ‘checkbox’); $header[] = t(’Name’); $header[] = t(’Category’); $header[] = t(’Discount’); $header[] = t(’Created on’); $output = theme(’table’, $header, $rows); $output .= drupal_render($form); return $output; } ?>
Add cancel button in your Drupal form
You can add the Cancel button or Cancel link in your Drupal form so you can cancel the form and go back previous page.
Code:-
<?php $form['submit'] = array( ‘#type’ => ’submit’, ‘#value’ => t(’Submit’), ); $form['cancel'] = array( ‘#type’ => ‘markup’, ‘#value’ => l(t(’Cancel’), ‘category’), ); ?>
This code add a “Cancel” link after “Submit” button.