Showing posts with label drupal. Show all posts
Showing posts with label drupal. Show all posts

Tuesday, August 30, 2011

Drupal - Simple captcha


Hi,


This article is about using custom captcha in Drupal. This one is a custom created module in Drupal. This is developed under Drupal 6.20 environment. I am sure if you get a feel of it you can modify it for other versions as well.

You can get the code from here.

This is how it would look like:


Code & Explanation:

After you have downloaded the module. Install it in your project. Once done that, all you have to do is to put the following lines of code in your page form:

Friday, July 8, 2011

Changing Drupal(6.2) theme on the fly

Hi,

This probably will be among my shortest post. The most commonly used module in Drupal for handling theme's is the ThemeKey module. But if you face a requirement where you have to change the theme of the page on the fly, then this post will be helpful.

To change the theme on the fly, you just need to write two lines of code i.e.

global $custom_theme;
$custom_theme="my_theme_name";

That's it. That is all that you have to do to change the page theme in Drupal.


Explanation:

Saturday, May 21, 2011

Drupal force login/logout

Hi,

This is not a very regular scenario, but I had got one, where I had to make a user auto login when they are coming to my website via a newsletter, that I had sent to the website members. The flow is, first I create various email newsletters and distribute it among my website users by mailing them. The newsletters would contain a link back to my website. Now when the users click that link, they come back to my website. They have to enter few basic details such as the password, gender, date of birth etc. When they click next they get logged in to the website.

I am using Drupal 6.20 for this development. The users that I am talking about are basically drupal users as well, so I need to login them in Drupal framework as well. It is actually simpler than I had thought. In Drupal whenever a user logs in it creates a global variable, "$user", instead of session variables to store the details of the logged in users. So all I had to do is to overwrite that variable with the user details who needs to be logged in.

This is the code that I had used:

$user_details=db_fetch_array(db_query("select * from {users} where id=<USER_ID>"));


$user_details->hostname=$_SERVER['REMOTE_ADDR'];
$user_details->timestamp=time();
$user_details->roles=array('4'=>'client');


global $user;
$user=$user_details;

Explanation:

The idea is very simple. You just need to overwrite the $user variable with that in the database. So I simply fetch all the details of the user and put it in the $user variable. But just by doing this will not do the job, I also need to assign few extra variables such as the hostname, timestamp and roles. Hostname and timestamp are pretty much easy and self explanatory. What is vital is the roles variable given as:

Wednesday, May 18, 2011

Multiple database in Drupal


Hi,

Recently I have been working with a distributed database architecture in Drupal 6, without the use of multisite. So thought of sharing in the blog. Multiple database is very important in Drupal when you have custom tables along with Drupal inbuilt tables. The best practice is to keep separate database for both. For creating multiple database in Drupal, first you need to follow the conventional Drupal installation process. One's that is done, create a seperate database and dump all the custom tables in that database.

Assume the name of the drupal database is "drupal_db" and the name of the custom database is "business_db". Now after you have successfully completed installing Drupal in your server, go to sites/default/settings.php file. There you will find the following or similiar looking lines:

$db_url = 'mysqli://root@localhost/drupal_db';
$db_prefix = 'drp_';

1st step to convert the current Drupal setup into multi database, is to replace the above two lines with the following:


$db_url['default'] = 'mysqli://root@localhost/drupal_db';
$db_prefix['default'] = 'drp_';

$db_url['business_data'] = 'mysqli://root@localhost/business_db';

Basically you just need to create an array of connections. In this, $db_url['default'] refers to the default connection if you are not explicitly mentioning from which database the data needs to be fetched. Most cases this is the drupal database. For the custom database you need to create another index of the same array with the details of that database. In this case I have named it as "business_data" in:

$db_url['business_data'] = 'mysqli://root@localhost/business_db';

Now the main part is to switch connections as needed. For that I had used db_set_active() function. Generally I have used this switching process just before and after performing any database transaction. This function takes 1 parameter, which is the array index name which I had created earlier i.e. "default" and "business_data".

e.g.

Thursday, February 17, 2011

Drupal Module 2

Hi,

Drupal provides an excellent multiligual support with minimum effort. I came across two such modules:

1. Google Translate
2. Language Icon

These are a whole bunch of them out there. But I liked these two. Google Translate uses conventional translation method for converting the entire page. It fetches data from google itself for the translation, so you cannot edit them. But in Language Icon you can specify translation of each word in Drupal dictionary. You can also create a language if you want. The only problem is you need to sit down and make the entire translation manually. But both of them rocks.

Other interesting modules are:

Wednesday, February 16, 2011

Drupal Module 1

Hi,


This is my first post in the blog. I would like to share a Drupal 6.2 modules which come in very handy on a daily basis. In a Drupal powered website it is very critical to manage different layout for different pages. But basic idea is, for every layout you just need to create seperate themes. Now if you do not know how to create one, this link is just what you need: