Posts Tagged Drupal
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.
How use AJAX in DRUPAL

How use AJAX in DRUPAL
We can easily use AJAX in DRUPAL framework. Drupal provide the jQuery javascript library so we can use jQuery for our AJAX implementation. First we write a module in which we are going to implement the server side logic. Suppose our module name is product and we will check the given product name is exist or not in our product table.
<?php /*product.module*/ function product_menu() { $items = array(); $items['product'] = array( 'page callback' => 'drupal_get_form', 'page arguments' => array('product'), 'access arguments' => TRUE, 'type' => MENU_CALLBACK, ); $items['product/check_name'] = array( 'page callback' => 'check_name', 'access arguments' => TRUE, 'type' => MENU_CALLBACK, ); return $items; } function product() { $path = drupal_get_path('module', 'product'); drupal_add_js($path . '/product.js', 'module'); $form['product_name'] = array( '#title' => t('Product Name'), '#type' => 'textfield', '#required' => TRUE, '#size' => 30, '#description' => t('Please enter product name.'), ); $form['check_name'] = array( '#type' => 'markup', '#value' => "<a href='#' id='check_name'>" . t('Check Product Name') . "</a><br/>", ); $form['status'] = array( '#type' => 'markup', '#value' => "<span id='status'></span><br/>", ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Submit'), ); $form['cancel'] = array( '#type' => 'markup', '#value' => l(t('Cancel'), 'product_mgmt'), ); return $form; } function check_name() { $name = strtolower($_GET['name']); $query = "SELECT COUNT(*) AS total FROM {product} WHERE LOWER(product_name) LIKE ('%s')"; $rs = db_query($query, $name); $info = db_fetch_object($rs); $total = $info->total; if ($total) { echo "$('#status').html('This product is available.');"; } else { echo "$('#status').html('This product is not available.');"; } } ?>
Don’t return anything in “check_name()” function otherwise it will return the whole page when we access “product/check_name” path through AJAX.
/*product.js*/ // JavaScript Document $(document).ready(function() { $('#check_name').attr('href', 'javascript:void(0);'); $('#edit-product-name').keydown(function(event){ $('#status').html(''); }); $('#check_name').click(function() { var name = $.trim($('#edit-product-name').val()); if (name == '') { $('#status').html('Please enter product name.'); return; } $.ajax({ type: "GET", url: "/product/check_name", data: "name=" + encodeURI(name), success: function(msg){ eval(msg); } }); }); });
Hide a Menu from Admin in Drupal
Suppose you want to create a Menu and don’t want to show it to Admin. Admin or Administrator is the first user of your Drupal website and it has all the access. Normally the user id (uid) of admin is 1. So, you can hide a menu from admin using the “access callback” in “menu_hook“. Lets say our module name is “test”.
<?php /*** Implementation of hook_menu(). */ function test_menu() { $items = array(); $items['earnings'] = array( 'title' => t('Earnings'), 'page callback' => 'earnings', 'access callback' => 'check_user', ); return $items; } function check_user() { global $user; if ($user->uid == 1) { return FALSE; } return TRUE; } ?>
Here we have used an “access callback” function “check_user()” for menu “earnings”. In “check_user()” function we have checked the uid of currently logged in user. If the currently logged in user is admin (uid = 1) then we return FALSE otherwise we returned TRUE.
Create a sortable table using “theme_table()” function in Drupal
Posted by admin in Drupal, Open Source on May 24th, 2009

Sortable table using theme_table() function
We can create a sortable header using “theme_table()” function. In sortable header user can sort a column in ascending or descending order. I am using a table called “package_coupon“. This table have seven fields (coupon, operator, operand, created, expire, used and user_by). We are providing the sorting facility on four fields only (coupon, created, expire and used). Initially, the coupon field is sorted by ascending order.
Here “tablesort_sql()” function is important. This function produces the ORDER BY clause to insert in your SQL queries, assuring that the returned database table rows match the sort order chosen by the user.
Code:-
function package_coupon_list() { $head = array( array('data' => t('Coupon'), 'field' => 'coupon', 'sort' => 'asc'), array('data' => t('Type of Discount')), array('data' => t('Created'), 'field' => 'created'), array('data' => t('Expire'), 'field' => 'expire'), array('data' => t('Status'), 'field' => 'used'), array('data' => t('Used by')), ); $sql = "SELECT * FROM package_coupon" . tablesort_sql($head); $result = db_query($sql); while ($coupon = db_fetch_object($result)) { $rows[] = array( array('data' => _coupon_format($coupon->coupon)), array('data' => ($coupon->operator == '%' ? "{$coupon->operand}% discount" : "\${$coupon->operator}{$coupon->operand} discount")), array('data' => format_date($coupon->created, 'small')), array('data' => format_date($coupon->expire, 'small')), array('data' => $coupon->used . ' left'), array('data' => ($coupon->user_by == '') ? 'None' : $coupon->user_by), ); } return theme_table($head, $rows); }
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"); }