Archive for category Open Source

Create Drupal form using theme_table() like module list form (Part II)

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)

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.

, ,

4 Comments


Add and show attachments in Joomla Article

Step1: 
         download this extension from the joomla webiste link is:
         <a href="http://extensions.joomla.org/extensions/3115/details"> Attachment </a>
 
Step2: 
         Unzip this extension and read the "INSTALL.txt" to how to install or upgrade this attachment.
 
Step3:
        A).
 
       To show attachments for all users, change some setting from the admin,
        go to: 
               1).  admin-&gt;components-&gt;article attachment then click on "parameters" 
                     look for  "Who can see attachments?" select "Anyone"
 
       B).
 
       To show attachment while article listing.
        1).
           file name :		
			\components\com_content\views\category\view.html.php
            Line no:
			after 157
	    add
			1:	$dispatcher	=&amp; JDispatcher::getInstance();	
 
	   Line no:
			after 191		
	   add
			2:				
				JPluginHelper::importPlugin('content');
				$results = $dispatcher-&gt;trigger('onPrepareContent', array (&amp; $item, &amp; $item-&gt;params, 0));	
				$item-&gt;event-&gt;afterDisplayContent = trim(implode("\n", $results));
 
      2).
           file name : \components\com_content\views\category\tmpl\default_items.php
        	Line no:
			where ever u want to show attachment file link
 
		add
			echo $item-&gt;text;

,

2 Comments


Create a sortable table using “theme_table()” function in Drupal

Sortable table using theme_table() function

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);
}

6 Comments


Create an action confirm form using “confirm_form()” function in Drupal

Doctor's List with delete option

Doctor's List with delete option


Action confirm form in Drupal

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");
}

, ,

15 Comments


Hide Input Format Options from Drupal Node Create Form

Normally you don’t want to show “Input Format” options available in node create form to your website users. You can easily hide them using CSS. We are hiding these options only from normal users and not from admin user.

Input Format

Input Format



1. Create a hook_form_FormID_alter callback function to add the ID to DIV that containing the Input Format options. Suppose our module name is “story” and we want to hide Input Format options from “Create Story” form. So, our callback function name should be story_form_story_node_form_alter(). Where “story_node_form” is Form ID of create story form.

function story_form_story_node_form_alter(&$form, &$form_state) {
	global $user;
 
	if ($user->uid != 1) {
		$form['format']['#attributes'] = array('id' => 'fieldset-input-format');
	}
}

2. We added the ID “fieldset-input-format” to DIV containing Input Format options. Now, we need to hide it using CSS.

#fieldset-input-format {
	display:none;
}

,

3 Comments