Archive for June, 2009

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


Getting Browser Information using JQuery

One day every browser will act in same way and support same web standards. However, that’s not today. In a sufficiently complicated web application, it’s important to know which browser user is using so we can know which JavaScript functions are available and which CSS properties it supports. Here, I am showing how you know about the users browser using JQuery. We can find lot of information about users browser like its name, version number etc through jQuery.

At the highest level, JQuery offers an object called browser that contains some simple flags to determine which one of the major browsers is currently being used – Safari, Opera, IE, or Mozilla.

   $(document).ready(function(){
 
    var browser;
    if($.browser.mozilla)
      browser = "Firefox";
    else if($.msie)
      browser = "Internet Explorer";
    else if($.browser.opera)
      browser = "Opera";
    else if($.browser.safari)
      browser = "Safari";
    else
      browser = "Unknown";
 
    $('#browserName').append(browser);
  });

Create a DIV so in your page so you can see the output of this JavaScript.

 <div id="browserName">Your Browser: </div>

When you execute the code, the output should look something like below. If you are using Safari.

Your Browser: Safari

,

1 Comment


How use AJAX in DRUPAL

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

, ,

16 Comments


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.

No 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