Showing posts with label Drupal. Show all posts
Showing posts with label Drupal. Show all posts

An Introduction to the Drupal Features Module

Most of the people get confused when they plan to use Drupal features module. I am giving few useful steps which will help you to create your own features. 

Features can be anything. A view, block, menus or content type etc.

You must be thinking that what is the actual use of features module. So I will a small example for your clarification.

For example you create a complex view in your local Drupal instance. Now you want to use the same view in development server or in some other server, than how will you do that? You will again create thw same complex view again in development server. Personally I would not suggest you to do that. Here comes a concept of features.

Steps- 

1. Once you are done with views creation than download features modules from here. Install and enable it. 
2. Go to the Structure->Features->Create Feature. 
3. Give a appropriate name for the view and select your view from drop down list.
4. Now at the end of page, click Download Feature. Once you download it, it will come as rar file which contains a module.
5. Next steps is to create one folder called features inside sites/all/modules folder so that you can differentiate  between your custom, contrib modules and features.
6. Paste your download feature inside sites/all/modules/features folder. Extract it and now this feature will be available in module list in admin. 
7. Enable this feature and it is ready to use.

Note- These above steps I explained for views example. These steps you can use for content type, blocks, menus etc.


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 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 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.

Create Table Structure with Pagination in Drupal Using theme table


For creating tables in drupal, you don’t need to write html table tag structure.
It is very easy to implement this in drupal.

Here I am giving the step by step explanation with code:-

1:-First create a menu in hook_menu() function.
In this menu we will display the table using theme table.
function hook_menu() {
    $items = array();
    $items['theme_table'] = array(
            'title' => t('Theme Table'),
            //calling custom test_theme_table() function on this menu
            'page callback' => 'test_theme_table',
            'access arguments' => array('access content'),
            'type' => MENU_CALLBACK,
            );    
    return $items;
    }
2:- Code for test_theme_table function:-
function test_theme_table(){
    $html = '';
 
    //this $header array contains table header name,can be changed as per the requirement
    $header = array(t('BID'),t('Module'),t('Delta'),t('THEME'),t('Status'));
 
    //$count is the number of rows you want to display per page
    $count = 5;                   
   
    //fetching records from blocks tabble
    $res = "SELECT * FROM {blocks}";
    
   //passing sql query and count value in drupal pager_query function
    $query = pager_query($res, $count);
    $data = array();
    while ($row = db_fetch_array($query)) {
      $data[] = array(
                      $row['bid'],
                      $row['module'],
                      $row['delta'],
                      $row['theme'],
                      $row['status']
                      );
    }
    //adding id to the table(Not required)
    $table_attributes = array('id' => 'example');
    $output = theme('table', $header, $data, $table_attributes);
 
    //returning the resultant table with $count=5
return $output.theme('pager', $count);
}
Instead of displaying records from database,you can display any records using them table.
  

How to create date popup in drupal custom module?

This is one of the most common feature that we need to implement in any project.

Here I am giving the step by step explanation with code.

1:- First download the date module from drupal.org.
2:-Enable the date & date popup module.
3:-Now go to your .module file (inside your custom module folder) then in hook_form function, and give #type=>date_popup.

Here is the code for the reference:-
        $format='d-m-Y';
        $form['date'] = array(
                '#type' => 'date_popup', //this is the main line of code that need to be add
                '#title' => t('Date'),
                '#date_format' => $format, //this date format can be changed as per the requirment
       );