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.

How to add seprate css for IE Browsers in Drupal Theme

It is very normal scenario  that your main css file is not affecting in IE browsers. For that, you will have to create separate css files and add those files in page.tpl.php inside your drupal theme.

Note:- Paste following code inside head tag in page.tpl.php file.
Code for adding css file in page.tpl.php :-

For IE6:-
<!--[if IE 6]>
     <link type="text/css" rel="stylesheet" media="all" href="<?php print $base_url;?>/sites/all/themes/splendapro/css/ie6_splenda.css" />
   <![endif]-->

For IE7:-
<!--[if IE 7]>
     <link type="text/css" rel="stylesheet" media="all" href="<?php print $base_url;?>/sites/all/themes/splendapro/css/ie7_splenda.css" />
   <![endif]-->

For IE8:-
<!--[if IE 8]>
     <link type="text/css" rel="stylesheet" media="all" href="<?php print $base_url;?>/sites/all/themes/splendapro/css/ie8_splenda.css" />
   <![endif]-->

Note:-The code above looks like commented but it is not.So use as it is.
 
For Safari browser:-
<?php    
   if (strpos($_SERVER['HTTP_USER_AGENT'], 'Safari') !== FALSE) {
          ?>
      <link type="text/css" rel="stylesheet" media="all" href="<?php print $base_url;?>/sites/all/themes/splendapro/css/safari.css" />
    <?php
    }
?>


Note:- Yo just need to change the css file path in above codes,and use the rest code as it is.