Wednesday, September 10, 2008

Disable drag/drop in Explorer (WinXP)

Drag/Drop in windows explorer is a great feature, but has its own drawbacks. An unintentional click and move of mouse on explorer window would move a folder somewhere and the user would not even notice it, but some applications/functionalities stop working or start giving vague errors. This is a pain, particularly with non technical users.

For a long time, I was searching for a way to disable drag/drop in windows explorer. There were some suggestions on customizing "local zone" in internet explorer settings, some registry tweaks, etc. But none of them worked, atleast for WinXP (SP2). Finally I found a software which could disable drag/drop -- and also do much more than that. Faronics WINSelect. It can disable lot of windows features and some features of third party apps too. This is an excellent tool to make a system less prone to unintentional user errors and is particularly useful if the machine is used by several users.

I found 2 issues with this software.

1. It is not free. However there is a 30 day evaluation version available.
2. Accorcing to the documentation, we have to uninstall it using Add/Remove programs. However, it does not create entries in Add/Remove programs, nor there is an uninstall utility in its installation directory or program group. Finally I figured out the way to uninstall. Run the installer again!

Thursday, August 28, 2008

Firefox 3 move tabs to bottom

Ok you were using firefox 2.0 with tab preferences extension. You had set the tabs to appear on the bottom. You upgraded your ff to 3.x. Now tab preferences extension anymore. There seems to be no easy way to move tabs to the bottom.

Or is there one?

There is. Just install Tab mix plus extension from mozilla addon site. There are lot of options for tabs, including moving it to bottom and undo closed tabs. It works with versions 1.0-3.0a5.

What about 3.0.1? Am I out of luck?

No. There is a dev version available in the author's website. You can find it here. Check the mozilla tab mix plus extension page before you install dev version. You might find a version released for 3.0.1 soon.

Friday, August 22, 2008

Programmatically create drupal term

Ever wanted to programmatically create drupal term? Here is the code snippet. Just call the below function by passing vocabulary id under which you want to create term.

function term_create($vid)
{
$edit = array ("vid" => $vid, "name" => $term_name);
taxonomy_save_term($edit);
}

Of course, you have to run this in a drupal environment, which means that you should have bootstrapped drupal before using the above function.

One issue with the above function is it uses vocabulary id, which we don't have/know most of the time. There is a quick solution. You can use below function to get vocabulary id from name.


function get_vocabulary_id ($vname)
{
$results = db_query('SELECT * FROM {vocabulary} WHERE name = "%s"', $vname);
if($results)
{
$res=db_fetch_object($results);
return $res->vid;
}
else
{
return null;
}
}

About drupal categories and subcategories

Drupal has an excellent way of classifying data. You can create a classification tree structures of whatever depth you want and arrange your data in the structures.

The problem: In the administration panel, you never see anything like classifications or categories.

Well, drupal calls it in a different name.

The root node of such structure is called vocabulary. The child nodes are called terms.

Administration (create/edit/update) of vocabulary and terms are available under Taxonomy.

Wednesday, August 13, 2008

Using date field in drupal forms

Adding a date field into drupal form is very easy. Just declare the form element to be of type 'date'. It automatically presents dropdowns for day, month and year next to each other neatly.

Example:

$form['date_of_birth'] = array(

'#type' => 'date',


'#title' => t('Date of Birth'),

'#default_value' => $node->expiry_date,

'#description' => t('Date of Birth')

);


Now we have the date field in our form. All is well till now -- until we submit the form.

Usually we use $form_values['values']['<field_name>'] to get the value and insert into the database. When we try to use the above value in the database INSERT INTO statement, it gives an error or does not insert the value.

So what is the problem? Actually $form_values['values']['date_of_birth'] is not a regular date string, but is an array containing day, month and year. So you have to create date string using the array before inserting into the database.

Here is an example to create a string suitable to insert into MySql date field:

$dobDateArray=$form_values['values']['date_of_birth'];
$date_of_birth_str=$dobDateArray['year'].'-'.
$dobDateArray['month'].'-'.$dobDateArray['day'];


Now you can use the $date_of_birth_str in the INSERT INTO statement.

Thursday, July 31, 2008

Writing your first Drupal custom module

How to write a drupal module to meet your custom requirements?
here is a simple guide. This assumes that you already have a working installation of drupal 6.x.

There are lot of tutorials available out there. I do not want to add one more. I just want to provide the developer a jump start by omitting most of the advanced stuff.

Let us write a module which does nothing but print the famous "Hello World" in the content area (not in the side bar) and call it "helloworld" module.

Now for directories and conventions. In the drupal installation directory, you will find "modules" directory. This is the directory which contains modules officially packaged with drupal. Don't mess this directory. You will also find a "sites" directory where installation specific modules need to be put. Under this directory, you will find "all" directory. The modules under this directory will be available for all sites catered by that particular installation (drupal can cater to multiple sites using one installation). Let us use this "all" directory. Create a directory "modules" under this. The "modules" directory will hold the custom modules. Create a directory "helloworld" under "modules". This will be the directory for our module files.

For our module to work, we need two files under this directory. info file and module file. info file contains information (such as id, version, etc) about your module and module file contains the code.

Let us create info file first. Create a file called "helloworld.info" under helloworld directory. Then write the below lines into that file.

; $Id$ Helloworld
name = Hello World
description = A simple module which prints Hello World on the screen.
core = 6.x
Now a little bit of explanation. First line is the identifier for your module. You can add date, version etc to it. Let us keep it simple for the time being. Second line is a name which appears in the title of the content for your module. Third line is a small description which appears in admin section. Fourth line tells that our module is compatible with drupal 6.x.

Now for the module file. Create a file called helloworld.module in the same directory. This file contains php code. The coding convention is to open the file with no end tag. That is right, no end tag.

The content for the module files goes here.

function helloworld_perm()
{
return array('access helloworld content');
}
The above function tells drupal to create permission control in admin section for our module for accessing content. Without this, users (except admin) will not be able to access the contents of the module.

function helloworld_content()
{
$content="Hello, World!";
return $content;
}
The above function returns the content of the module. In our case, it is a simple string "Hello, World!".

function helloworld_menu()
{
$items = array();
$items['helloworld'] = array( 'title' => 'Hello World', 'page callback' => 'helloworld_content', 'access arguments' => array('access helloworld content'), 'type' => MENU_CALLBACK );

return $items;
}
This is the last function in the module file. This will add a callback which enables us to access our module contents using url like /index.php?q=helloworld or /helloworld.

Put all these 3 functions in the module file after
Now, login to drupal as admin. Go to modules under site building. At the end of the modules list, you should find your module. Enable the module by checking the checkbox and clicking "save configuration" button.

Now go to permissions under "User management". You should find a section "helloworld module". Check "authenticated user" checkbox against "access helloworld content". Configuration for our module is ready.

Logoff admin and login as a regular user. Access the URL /helloworld or /index.php?q=helloworld (depending on whether you have enabled smart url or not). You should see "Hello, World!" in the content pane.

About drupal custom modules

There are lot of CMSs (Content management system) out there, most popular currently being Drupal and Joomla.
There are lots of debates over which of these CMS is better. In my
opinion, each have their own strength and weaknesses. But when it comes
to power of customization, I choose Drupal over Joomla.

Ok, so we have a drupal powered website and we need some specific functionality which drupal does not provide out-of-box. So what do we do? Write our own module?

No! Before getting into development of custom module, I recommend searching in the modules contributed by users on drupal's website. Most of the times, you find one which suits your requirements, and sometimes you will be pleasantly surprised by the additional features it may have.

Why? What is the problem with me developing my own module?
Ok, here is why.
1. It saves a lot of time you put into development.
2. User contributed modules would have been tested by several people, issues detected and fixed. And hence will be more stable.
3. You will be able to get help/info on the module in the drupal forum.
4. To cut it short, no point reinventing the wheel.

Suppose you didn't find a suitable module or you just want to learn how to write a drupal module, check my other post here
Writing your first Drupal custom module.