Monday, 3 October 2011

simple drupal "hello world" block custom modules


this is just a really fast and crude way to printout the content in creating a drupal custom modules

 module name "hello_world"

first step is to create a "info" file for your modules ""

and paste in the before code

 ; $Id$  
 name = hello_world  
 description = Custom functions for this site.  
 core = 6.x  

next step is to create a module file this is the file where all the logic goes
create a file "hello_world.module" and paste in the below code

 // $Id: hello_world.module  
 * @file* Custom functions for this site.  
  //implementing the hook_block functions   
 function hello_world_block($op = 'list',$delta = 0, $edit =array()){  
 if($op == 'list'){ //listing the blocks  
 'info'=> t('hello world block'),  
 'weight' => 0,  
 return $blocks;  
 //loading the content of the blocks   
 if($op == 'view'){  
 if($delta == 0){  
 $blocks = array(  
 'subject' => t('hello world blocks'),  
 'content' => '<h1>hello world</h1>';// alternatively you can call a function which return the data  
 return $blocks;  

this above code can be used to quickly create block to display a banner or ad-script in a block or a even iframe.

Sunday, 2 October 2011

author information in a drupal block


here is simple php snippet used to show the author picture and name in a block .

ok this is how this works it collect the node-nid from the url. load's the node get the author uid and then load the user and print the information about the user

 //url example{nid}  
 if(isnumeric(arg(1)){ //  
 $node=node_load(arg(1)); // using the node load function  
 if($node){ // checking whether node object is loaded  
 $author = user_load($node->uid);//using the user load function to load the user object  
 echo '<div>';  
 echo '<img src="'.$author->picture.'" /><br/>'; // print the path to the user iamge  
 echo '<b>'.$author->name.'</b><br/>'; //print the user name  
 echo '<b>'.$author->mail.'</b><br/>'; // print the email address of the user  
 echo '</div>';  

this might be very crude way of doing this. alternatively you can create view to do the same

Thursday, 18 August 2011

php code to redirect the users according to roles in drupal


this is a  simple and crude php code to redirect the user according to roles in drupal

this can be used in drupal hook_user function or panels pages to redirect the user

 global $user;  
 //consider two roles moderator and regular user  
 //moderator role rid is 2  
 //regular user role rid is 4  
 //roles item is array  
 if($user->roles[2]){// if user has a role moderator  
 //do this  
 drupal_goto("user/moderater-panel");// redirecting  
 }elseif ($user->roles[4]){ // user user have role regular user  
 drupal_goto("user/regular-panel");// redirecting  
 }else{ // if the user have other roles then above roles do this  
 //do smothing  

Monday, 7 March 2011

a simple perl timer scripts


here is some simple code to shutdown the computer at a particular probably may have more better script then this. or even write simple shell script. but here is what i use.

 $cont = "075500"; // time at which the computer must halt 07:55:00  
 $var = `date +%H%M%S`;//initializing the $var and getting the current time from the shell command and storing the value in $var  
 if($var == $cont) //checking the values  
 `init 0`//signal command to shut down the computer.  
 `sleep 1`;  
 $var = `date +%H%M%S`;//after every second getting the current time and matching the value in a infinite while loop.  

as halt command need super user privilege. you need to run the script as root or sudoer 

Thursday, 3 March 2011

getting a mutual friend list using custom sql query in drupal


i am using drupal UR module to implement the friends relations.below sql query is used to display mutual friend list. create custom module use this sql query and get the mutual friend list.

(SELECT user_relationships.rid AS rid, AS users_user_relationships_name, users_user_relationships.uid AS users_user_relationships_uid, user_relationship_types_user_relationships.plural_name AS user_relationship_types_user_relationships_plural_name FROM user_relationships user_relationships LEFT JOIN users users_user_relationships ON user_relationships.requestee_id = users_user_relationships.uid LEFT JOIN users users_user_relationships_1 ON user_relationships.requester_id = users_user_relationships_1.uid LEFT JOIN user_relationship_types user_relationship_types_user_relationships ON user_relationships.rtid = user_relationship_types_user_relationships.rtid WHERE (user_relationships.approved = '$user->uid') AND (user_relationships.requester_id = arg(1)) OR (user_relationships.requester_id = build) GROUP BY users_user_relationships_uid HAVING count(*) = '2' ORDER BY user_relationship_types_user_relationships_plural_name ASC )