Monday, July 21, 2014

Drupal 7 ajax without jquery using use-ajax

Using "use-ajax" attribute, we can achieve ajax in Drupal.
Follow below steps to do it.

Steps :
1) hook_menu entry
2) function definition to declare ajax element (Hyperlink)
3) Ajax definition

1) hook_menu entry.
function yourModuleName_menu() {
  $items = array();
  $items['Mymodule/ajax'] = array(
    'page callback' => 'Mymodule_ajax_test',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  $items['Mymodule/ajax/%/%'] = array(
    'page callback' => 'Mymodule_ajax_callback',
    'page arguments' => array(2, 3),
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  return $items;
}

2) function definition to declare ajax element (Hyperlink).
function Mymodule_ajax_test() {
  drupal_add_library('system', 'drupal.ajax');
  drupal_add_library('system', 'jquery.form');
  $arg = 'arg';
  $output = l(t('AJAX'), 'pm/ajax/nojs/' . $arg, array('attributes' => array('class' => array('use-ajax'))));
  $output .='<div id="some-wrapper"></div>'; // udadate result here.
  return $output;
}
// Another method.
function Mymodule_ajax_test() {
  drupal_add_library('system', 'drupal.ajax');
  drupal_add_library('system', 'jquery.form');
  $some_argument = 'arg';
  $output = l(t('AJAX'), 'Mymodule/ajax/nojs/' . $arg, array('attributes' => array('class' => array('use-ajax'), 'id' => array('some-wrapper')))); // overwrite result here.
  return $output;
}

3)  Ajax definition.
function Mymodule_ajax_callback($type = 'ajax', $arg) {

 if ($type == 'ajax') {
    $commands[] = ajax_command_replace('#some-wrapper', 'Ajax result '  );
    $page = array('#type' => 'ajax', '#commands' => $commands);
    ajax_deliver($page);
  }
  else {
    $output = t("Ajax fail or page load contents.");
    return $output;
  }
}

Friday, July 18, 2014

Drupal 7 ajax form validation and submit

Ajax is one of the best interactive way to validate and submit Drupal custom form.
Using below steps, we can achieve ajax form validation and submit.

Steps :
1) Declare form elements
2) Call back definitions.

1) Declare form elements.
$form['submit'] = array(
  '#type' => 'submit',
  '#value' => t('SUBMIT'),
  '#validate' => array('validate_callback'), // custom form validate.
  '#ajax' => array(
    'callback' => 'submit_callback',
    'effect' => 'fade',
    'wrapper' => 'specific-wrapper',
    'event' => 'click',
    'progress' => array('message' => '', 'type' => 'throbber'),
  ),
);

2) Call back definitions.
function submit_callback($form, $form_state) {
  if (form_get_errors()) {
    $form_state['rebuild'] = TRUE;
    return $form;
  }

  $response = my_form_submit($form, $form_state); // write your form submit logic here.
  return $response;
}
"validate_callback" is our normal hook_form_validate(). 

NOTE : Reach me through the contact form if you have any question.

Wednesday, June 4, 2014

Drupal date popup in custom form

Lets we see how to integrate date_popup on drupal custom form.
We can achieve it using drupal core functionality.
No need to go for jquery plugin.

Follow below 3 steps to achieve it :
1. custom form entry
2. hook_element_info_alter
3. Js for settings.

1. Create field in your custom form :
$form['birth_date'] = array(
 '#type' => 'date_popup',
 '#date_format' => 'd-m-Y',
 '#title' => t('Birth Date'),
 '#required' => TRUE,
 '#attributes' => array('class' => array('date-picker')),
);

2. hook_element_info_alter() to alter date_popup field. :
// For example :
date_popup by default come with time field. You can hide it.
Similarly you can hide description , title , etc ...
function myModule_element_info_alter(&$type) {
  if (isset($type['date_popup'])) {
    $type['date_popup']['#process'][] = 'myModule_date_popup_process';
  }
}

function myModule_date_popup_process($element, $form_state, $complete_form) { unset($element['date']['#description']); unset($element['date']['#title']); return $element; }
3. Add below JS file settings. :
(function ($) {
  Drupal.behaviors.custom_js = {
    attach: function(context) {
      $('.date-picker').datepicker({   
        showOn: "both", // Another one option is "button".
        buttonImage: Drupal.settings.datePicker.iconPath + '/images/datepicker-icon.png',
        buttonImageOnly: true,
        dateFormat: "dd-mm-yy",
        changeMonth : true, // To edit month field.
        changeYear : true, // To edit date field.
        yearRange: "-120:",
      }); 
    }
  };
})(jQuery);


Final step is to create Drupal settings to hold theme_path to hold date picker icon image.
// We can get theme path using Drupal settings variable.
global $theme_path;
drupal_add_js(array('datePicker' => array('iconPath' => $theme_path)), 'setting');

Wednesday, May 14, 2014

Autocomplete Drupal Form Tutorial.

I got a chance to use drupal form '#autocomplete_path' attribute to my one of the project.

Example usage :
// Autocomplete field
$form['autocomplete'] = array(

 '#type' => 'textfield',

 '#title' => 'Autocomplete field',

 '#description' => 'Autocomplete field example',

 '#autocomplete_path' => 'article/autocomplete',

  );


I have found one good resource about it at http://www.coalmarch.com/blog/autocomplete-drupal-form-tutorial

It may be useful to you. Happy coding :)

Thursday, April 24, 2014

Drupal 7 theme two custom forms in a tpl file or a single page


Let's we discuss How to theme two Drupal 7 custom forms in tpl file or in a single page.

First I would like to thank this guy for his post.

Steps :
1. Create menu call back for landing page where do you want to display two forms.
2. Create two forms.
3. Link two forms into theme function.
4. Write logic into hook_theme.

1. First step to create menu call back for landing page.
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.Create Two forms.
// First form.
function custom_salesforce_login_form($form, &$form_state) {
$form['rtc_registered_email'] = array(
'#type' => 'textfield',
'#required' => TRUE,
'#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' => TRUE,
'#title' => t('Registered Email 2'),
);
$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;
}

3. Link two forms into theme function.
 //Link your both forms into theme() function.  You can also pass form elements if its necessary. 
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; 
}

4. Write logic into hook_theme.
 /**
 * Implements hook_theme(). 
 */ 
function moduleName_theme($existing, $type, $theme, $path) {
$items['custom_salesforce_update_form'] = array(
 'render element' => 'form', 
 'template' => 'custom_salesforce_update_form',
 'path' => drupal_get_path('module', 'moduleName') .'/template', );
 $items['custom_salesforce_login_form'] = array( 
 'render element' => 'form', 
 'template' => 'custom_salesforce_login_form',
 'path' => drupal_get_path('module', 'moduleName') .'/template', ); $items['custom_salesforce_login_and_update'] = array(
 'template' => 'custom_salesforce_login_and_update', 
 'path' => drupal_get_path('module', 'moduleName') .'/template',
 'arguments' => array('combine_form' => NULL), ); 
 return $items;
 }

/**
 * 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']);
}


5. custom_salesforce_login_and_update.tpl.php
//Print your both forms in the above tpl file.
 <div class="login-form-fields parent">
<?php print $arg_return_couple_form; ?>
 </div> <div class="login-form-fields parent"> 
<?php print $arg_first_time_login_form; ?> 
</div>

Note : Create tpl files for those two forms separately and print form elements as your wish. So totally three tpl files are needed.




Friday, October 25, 2013

Drupal backup and restore using Drush

 

Drush is a very poweful command line tool to manage Drupal efficiently.

You can manage your drupal site very well using this tool. 

Starting from Drupal installation, you can 

1) Download themes and modules
2) Enable themes and modules
3) Disable themes and modules
4) uninstall modules
5) Clearing cache
6) Backup root files & database
7) Restore them to new site

Some useful drush commands :

Before running the command, first go to desired directory.

Ex: If you want to enable commerce module, first go to /var/www/docroot/sites/all/modules/contrib
drush dl commerce  - will download the commerce module.
drush en commerce - will enable it
drush uninstall commerce - will uninstall it
drush dis commerce - will disable it&nbsp
drush cc all - will clear all cache&nbsp

Backup and restore :

Backup:

As i said before go to your root directory before running below command.
drush archive-dump --destination=/var/back_up/yoursite_today.com.tar.gz

( or )

drush ard --destination=/var/back_up/yoursite_today.com.tar.gz



 Restore :

drush archive-restore /var/back_up/yoursite_today.com.tar.gz --destination=/var/www/new_site.com

( or )

drush arr /var/back_up/yoursite_today.com.tar.gz --destination=/var/www/new_site.com

When you go to your backup directory, there will be one MANIFEST.ini which will contain backup details as in below.

[Global]
datestamp = "1382704323"
formatversion = "1.0"
generator = "Drush archive-dump"
generatorversion = "6.0-beta1"
archiveformat = "platform"

[default]
docroot = "/var/www/beta_bbd"
sitedir = "sites/default"
files-public = "sites/default/files"
database-default-file = "beta_bbd.sql"
database-default-driver = "mysql"
 
Click here to see more about Drush . Another useful link.

Wednesday, July 17, 2013

Apache Solr Search Integration in Drupal



This article explains how to use Apache Solr as search engine for a Drupal 7 site (it also applies to Drupal 6). I assumes that you are running Debian 6, but the description can also be used on any other Linux based distribution.

Apache Solr is written in Java and we need to start by installing Java, You should note that Apache Solr can be run in a Tomcat installation I do however prefer to not use tomcat, mostly because I don't have much experiences with tomcat.

1) Click here for install and configure  2) This link gives more detailed view about it