How to create Multiple Forms in Drupal 7

After spending couple of hours finally I got solution to create multiple custom forms and display on single page with proper UI.
Follow the below steps to create the same-
1. First of all create a menu item in hook_menu() where you want to display forms.
function example_menu() {
  $items['user-login'] = array(
    'title' => 'Multiple Forms on Single Page',
    'page callback' => 'custom_salesforce_user_login_page',
    'access callback' => 'user_is_anonymous',
  );
  return $items;
}

2. Second step is to write a callback function which is written in hook_menu(). Inside this callback, write all the forms which you want to display on one page.
function custom_salesforce_user_login_page() {
  $custom_salesforce_login_form = drupal_get_form('custom_salesforce_login_form');
  $custom_salesforce_update_form = drupal_get_form('custom_salesforce_update_form');
  $combine_form = array('arg1' => $custom_salesforce_login_form, 'arg2' => $custom_salesforce_update_form);
  $output = theme('custom_salesforce_login_and_update', $combine_form);
  return $output;
}

3. Now create these two forms.
First Form-
function custom_salesforce_login_form($form, &$form_state) {
  $form['rtc_registered_email'] = array(
    '#type' => 'textfield',
    '#required' => FALSE,
    '#title' => t('Registered Email'),
  );
  $form['rtc_password'] = array(
    '#type' => 'password',
    '#required' => FALSE,
    '#title' => t('Password'),
  );
  $form['rtc_submit'] = array(
    '#type' => 'submit',
    '#id' => 'salesforce_ret_couple_login',
    '#value' => t('Login'),
  );
  return $form;
}

Second Form-
function custom_salesforce_update_form($form, &$form_state) {
  $form['fth_registered_email'] = array(
    '#type' => 'textfield',
    '#required' => FALSE,
    '#title' => t('Registered Email'),
  );
  $form['fth_create_password'] = array(
    '#type' => 'password',
    '#required' => FALSE,
    '#title' => t('Password'),
  );
  $form['fth_confirm_password'] = array(
    '#type' => 'password',
    '#required' => FALSE,
    '#title' => t('Confirm Password'),
  );
  $form['fth_submit'] = array(
    '#type' => 'submit',
    '#id' => 'salesforce_fth_login',
    '#value' => t('Create Password'),
  );
  return $form;
}

4. Next step is to create a hook_theme() function to theame these forms.
function custom_salesforce_theme() {
  return array(
 // this template is containing theme style for update form
    'custom_salesforce_update_form' => array(
      'template' => 'theme/custom_salesforce_update_form',
      'render element' => 'form',
    ),
 // this template is containing theme style for login form
    'custom_salesforce_login_form' => array(
      'template' => 'theme/custom_salesforce_login_form',
      'render element' => 'form',
    ),
 // this template is containing theme style for both forms
    'custom_salesforce_login_and_update' => array(
      'template' => 'theme/custom_salesforce_login_and_update',
      'arguments' => array('combine_form' => NULL),
    ),
  );
}


Note- Don't forget to create three template files inside theme folder inside your module.
5. Next step to write template preprocessor functions to display individual fields in separate template files.
/**
 * Implements Template Preprocessor For User Login().
 */

function template_preprocess_custom_salesforce_login_form(&$variables) {
  $variables['rtc_registered_email'] = drupal_render($variables['form']['rtc_registered_email']);
  $variables['rtc_password'] = drupal_render($variables['form']['rtc_password']);
  $variables['rtc_submit_form'] = drupal_render_children($variables['form']);
}

Note:- Use these above variable names with "$" inside respective templates and give design as you want. For example use below html code for inside 
custom_salesforce_login_form template.
<html>
<head>
</head>
<body>
<div class="login-form-fields">
<?php print $rtc_registered_email; ?>
</div>
<div class="login-form-fields">
<?php print $rtc_password; ?>
</div>
<div class="login-form-fields">
<?php print $rtc_submit_form; ?>
</div>
</body>
</html>


Follow the same procedure inside custom_salesforce_update_form template for second form also.Just change the variable names as defined below.
/**
 * Implements Template Preprocessor For Update User().
 */

function template_preprocess_custom_salesforce_update_form(&$variables) {
  $variables['fth_registered_email'] = drupal_render($variables['form']['fth_registered_email']);
  $variables['fth_create_password'] = drupal_render($variables['form']['fth_create_password']);
  $variables['fth_confirm_password'] = drupal_render($variables['form']['fth_confirm_password']);
  $variables['fth_submit'] = drupal_render_children($variables['form']);
}


Now we need to display above two forms together on one page. For that just print these two below variables in custom_salesforce_login_and_update template. 
Examples-
<html>
<head>
</head>
<body>
<div class="login-form-fields">
<?php print $arg_return_couple_form; ?>
</div>
<div class="login-form-fields">
<?php print $arg_first_time_login_form; ?>
</div>
</body>
</html>

/**
 * Implements Template Preprocessor For Login and Update User().
 */

function template_preprocess_
custom_salesforce_login_and_update(&$variables) {
  $variables['arg_return_couple_form'] = drupal_render($variables['arg1']);
  $variables['arg_first_time_login_form'] = drupal_render($variables['arg2']);
}

How to install Drupal using Drush command line?

Installing Drupal using web browser is very hectic process. Every time you want to install new Drupal instance, download the latest Drupal version, copy paste it inside your working directory, open the browser and do etc etc etc...

I have a better solution for this.

If you have installed Drush in your machine then follow the below steps. Otherwise go to my this post and first installed Drush after that continue with this post.

You just need to go to your working directory for example (c:/wamp/www Or c:/xampp/htdocs) using your drush command line interface. Then run these two commands one by one.

This command will download drupal 7 inside your working directory.
Note - Make sure your machine is connected with internet.
drush dl drupal-7.x 

In below command, change "YourMySQLUser" with your Mysql username, "RandomPassword" with your Mysql  password and "YourMySQLDatabase" with your database name.
Note - Make sure your create database before running this command.
drush site-install standard --account-name=admin --account-pass=admin --db-url=mysql://YourMySQLUser:RandomPassword@localhost/YourMySQLDatabase

That's All. Your Drupal installation is completed. You don't even need to open any web browser. Just run these two commands and enjoy :)

How to install drush command line utility on Windows?

Installing Drush command line on Windows is very easy but sometimes it gives these errors such as
"Drush is recognized as internal or external command" Or "Php is not recognized by Drush"

In this situation please follow these steps to setup Drush perfectly on your Machine.

Step 1-
Download the drush from here, unzip it and copy paste it in c drive.

Step 2-
Install the following software's in default c: drive:-
gzip-1.3.12-1-setup(from here)
libarchive-2.4.12-1-setup(from here)
tar-1.13-1-bin(from here)
wget-1.11.4-1-setup(from here)

Step 3-
Download Drush Installer msi file from here
and Install.

Step 4-
Set the environment variable for drush and php location.
1. Go to my computer->Properties->Advanced->Environment Variables.
2. Edit the existing path and paste the following code at the end
   For wamp server -
  ;C:\wamp\bin\php\php5.4.3;C:\drush;C:\ProgramFiles\GnuWin32\bin;
  

   For xampp server -
  ;C:\xampp\php;C:\drush;C:\Program Files\GnuWin32\bin;

Step 5-
Start the drush command line interface and type following command:-
>drush status

You following links to execute Drush commands.

How to implement Facebook Like funtionality in drupal?

There are two ways to implement Facebook likes functionality in your project.

1. First of all it requires a Facebook Url for which you want to display likes count.
    Example:- www.facebook.com/xyz  //Here xyz could be anything.

2. If you want like count numbers with like button in your website then you need to get code from here. Provide the Url(Ex. www.facebook.com/xyz) and select other options as par your requirement.

3. Now Click get code.You will get 2 codes. First one is the Java-script code. You need to put this code in your html head tag.

4. The other code you need to put wherever you want to display like button and its count.

The requirement which I got to implement Facebook like functionality was bit different. I wanted to display only Facebook like count, and not to display like button. For this I have these solutions:-

1. You should use FQL(Facebook Query Language) here. Use the following code:-
https://api.facebook.com/method/fql.query?query=select like_count from link_stat where
url = “http://www.facebook.com/xyz”    // Change this Url

2. Pass this Url inside drupal_http_request() function and use json_decode() to get the value of like count. Then display it wherever you want.

3.You can get the like count by using this code also.
https://graph.facebook.com/?id=your_fb_page_id  // This id should be your page id in Facebook

4. Pass this Url inside drupal_http_request() function and use json_decode() to get the value of like count. Then display it wherever you want.

   

How to a send mail in Drupal?

To send a mail, we will use drupal_mail() function.
Here I am giving you an example, how you can send mail.

call this function where you want to send mail.
/*
  here test is the name of function, first_mail is the case value we are passing for switch case.
  $toEmail is the email id to whom you want to send mail, language is English and $params is the array of     parameters you want to send in mail.
 */
drupal_mail ('test', 'first_mail', $toEmail, "English", $params);
 
// The same way pass other case values to send some other mail.
drupal_mail ('test', 'second_mail', $toEmail, "English", $params);

Here we are writing mail function.
 
function test_mail($key, &$message, $params) {
$headers = array(
                        'Content-Type' => ' text/html'  // this is the header of the mail
                    );
$headers['From']="test@test.com";   // Write the email id from which you want to send the mails
$message['headers'] = $headers;
    switch($key) {
    case 'first_mail':    //case value which we are passing in drupal_mail
        $subject="Write Your Mail Subject";  // Write the subject of the mail here
        $body="";   // Write the body of your mail
        $message['subject'] = t($subject);
        $message['body'][] = $body;
    break;
    case 'second_mail':
        $subject="Write Your Mail Subject";  // Write the subject of the mail here
        $body="";   // Write the body of your mail
        $message['subject'] = t($subject);
        $message['body'][] = $body;
    break;
    }
}

How to create dependent drop down in custom form using Ahah module in Drupal?

Here I am explaining step by step, how you can create dependent drop down.

Please go through the code and read the comment.

function example_ahah_form( $form_state ) {
  $form = array();
 
  // this is standard method for register ahah helper for current form
  ahah_helper_register($form, $form_state);
 
  // provide default option for select one
  $select1_selected = 1;
 
  // $form_state['storage'], contains ahah submitted values
  if ( isset($form_state['storage']['dependent_select']['select1']) ) {
 
     // get 'select1' selected option
    $select1_selected = $form_state['storage']['dependent_select']['select1'] ;
  }
 
  // build select2 options depending upon select1
  if ( $select1_selected == 1 ) {
     $select2_options['11'] = 'option1 [select1]';
     $select2_options['12'] = 'option2 [select1]';
  }
  else if ( $select1_selected == 2 ) {
    $select2_options['21'] = 'option1 [select2]';
    $select2_options['22'] = 'option2 [select2]';
  }
  $form['dependent_select']['select1'] =
    array(
      '#type' => 'select',
      '#title' => t('Select 1'),
      '#options' => array( 1=> 'option1', 2 => 'option2'),
      '#default_value' => array( $select1_selected ),
     
      // specify ahah event
      '#ahah' => array(
      'event' => 'change', // this is onchange event of select
      'path' => ahah_helper_path(array('dependent_select')),
     
      // provide temporary path
      // no need to register it through hook_menu
      'wrapper' => 'dependent-select-wrapper',
     
      // provide the wrapper of element
      // here we have given the id of fieldset
     ),
   );
    $form['dependent_select']['select2'] =
    array(
      '#type' => 'select',
      '#title' => t('Select 2'),
      '#options' => $select2_options // set dynamic options
    );
    $form['submit'] =
    array( '#type' => 'submit',
      '#value' => t('Save')
      );
  return $form;
}

How to create block in custom module in Drupal?

1. First of all you need to use hook_block() to create block in custom module.
2. See the below code for creating block.

function apqc_benchmarking_block($op = 'list', $delta = 0, $edit = array()) {
  if ($op == 'list') {
    $blocks[1]['info'] = t('First Block');
    $blocks[1]['cache'] = BLOCK_NO_CACHE;
    $blocks[2]['info'] = t('Second Block');
    $blocks[2]['cache'] = BLOCK_NO_CACHE;

    return $blocks;
  }
  elseif ($op == 'view') {
    switch ($delta) {
      case 1:
        $block['subject'] = t('First Block');
        $block['content'] = theme('apqc_benchmarking_first_time_here');
        break;
      case 2:
        $block['subject'] = t('Second Block');
        $block['content'] = call_any_function();
        break;
    }
    return $block;
  }
}

In above code, I have created two blocks named First Block and Second Block.
In $op='list', I am listing the block names. You can add multiple blocks here.
In $op='view', You can display whatever you want to show.
For example, you can call any theme function or any function which is returning any HTML content.