Monday, September 22, 2014

Drupal batch process for custom function callback

Drush is a very powerful command line tool to manage Drupal site.

Other than core Drush command, we can also write our own custom drush command to perform some task like cron setup, etc, ...

we can do it by using below 3 steps.

Steps :
1) Write hook_drush_help.
2) Write hook_drush_command.
3) Write drush_hook_command.

1) hook_drush_help.
/**
 * Implements hook_drush_help().
 */
function my_module_drush_help($command) {
  switch ($command) {
    case 'drush:test-import':
     return dt('Import test contents.');
  }
}

2) hook_drush_command.
/**
 * Implements hook_drush_command().
 */
function my_module_drush_command() {
  $items = array();
  $items['test-import'] = array(
    'description' => dt('Import test contents.'),
    'arguments'   => array(
      'arg1'    => dt('An optional example argument'),
    ),
    'examples' => array(
      'Standard example' => 'drush test-import',
      'Argument example' => 'drush test-import 5',
    ),
    'aliases' => array('ti'),
  );
  return $items;
}

3) drush_hook_command().
/**
 * Implements drush_hook_command().
 */
function drush_my_module_test_import($arg = NULL) {
  batch_custom_function();
}

4) batch_custom_function().
/**
 * Batch start function. In this example, I'm downloading XML file.
 */
function batch_custom_function() {
  $batch = array(
    'title' => t('Download XML file.'),
    'operations' => array(
      array('_batch_xml_download', array()),
    ),
    'progress_message' => t('Downloading XML file ...'),
    'error_message' => t('Error! while downloading XML file ...'),
    'finished' => '_batch_download_finished',
  );
  //Get the batch process all ready!
  batch_set($batch);
  $batch = &batch_get();

  //Because we are doing this on the back-end, we set progressive to false.
  $batch['progressive'] = FALSE;

  //Start processing the batch operations.
  drush_backend_batch_process();
}

5) _batch_xml_download callback.
/**
 * XML download.
 */
function _batch_xml_download(&$context) {
  $xml_read_return = '';
  $context['message'] = 'Downloading XML file ...';
  $xml_read_return = custom_callback_to_download_xml();

  if($xml_read_return == 200) {
    drush_log('XML file downloaded and saved successfully.', 'ok');
  }
  elseif (!empty($xml_read_return)) {
    drush_log('Unable to download XML file. (' . $xml_read_return . ')', 'error');
  }
}

6) _batch_download_finished callback.
/**
 * Finish of butch. Messagess
 */
function _batch_download_finished($success, $results, $operations) {
  if ($success) {
    drupal_set_message(t('Your success msg.'));
  }
  else {
    drupal_set_message(t('An error occurred while processing operation.'));
  }
}

Friday, September 19, 2014

PHP create XML file

Pragmatically create XML using PHP.

  $xml = "<company>";
  $xml .= "<employee>";
  $xml .= "<id>100</id>";  
  $xml .= "<name>name</name>";
  $xml .= "<email>email</email>";  
  $xml .= "<salary>salary</salary>";    
  $xml .= "</employee>";  
  $xml .= "</company>";
  $xml_obj = new SimpleXMLElement($xml);
  $xml_obj->asXML('public://myfile.xml'); // Drupal file path.


Wednesday, August 27, 2014

Drupal feeds batch import - Feeds batch process

Feeds module is a one of the best module to import nodes from external URL, xls file or from  xml file, etc..

After creating feeds import for particular action, we can run it via batch process. Like via cron instead of running it manually.

I will create new post to create about feeds import.

Below line of codes worked perfect for one of  my project:

$file = 'your-xml-file.xml';
$my_importer = feeds_source('your_importer_name');
$config = array('FeedsFileFetcher'=>array('source'=>'sites/default/files/' . $file));
$my_importer->addConfig($config);
while (FEEDS_BATCH_COMPLETE != $my_importer->import());

use below link to integrate above batch process via cron.

http://kali-dasan.blogspot.in/2014/08/drush-custom-command-drush-in-our.html

Tuesday, August 19, 2014

Drush custom command - Drush in our custom module

Drush is a very powerful command line tool to manage Drupal site.

Other than core Drush command, we can also write our own custom drush command to perform some task like cron setup, etc, ...

we can do it by using below 3 steps.

Steps :
1) Write hook_drush_help.
2) Write hook_drush_command.
3) Write drush_hook_command.

1) hook_drush_help.
/**
 * Implements hook_drush_help().
 */
function my_module_drush_help($command) {
  switch ($command) {
    case 'drush:test-import':
     return dt('Import test contents.');
  }
}

2) hook_drush_command.
/**
 * Implements hook_drush_command().
 */
function my_module_drush_command() {
  $items = array();
  $items['test-import'] = array(
    'description' => dt('Import test contents.'),
    'arguments'   => array(
      'arg1'    => dt('An optional example argument'),
    ),
    'examples' => array(
      'Standard example' => 'drush test-import',
      'Argument example' => 'drush test-import 5',
    ),
    'aliases' => array('ti'),
  );
  return $items;
}

3) drush_hook_command.
/**
 * Implements drush_hook_command().
 */
function drush_my_module_test_import($arg = NULL) {
  // print arg if any.
  if (isset($arg)) {
   drush_print($arg);
  } 
  drush_log('importing test contents...', 'ok');
}

Now Goto drush command line interface and try > drush test-import or > drush ti

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');