Wednesday, January 28, 2015

Print an array or object in watchdog Drupal

Use below lines of code to print an array or object in watchdog  Drupal.

// Directly use values.
watchdog("log_name", '<pre>' . print_r( $my_object, true) . '</pre>');


// With place holder as per Drupal standard.
watchdog('log_name', '<pre>@place_holder</pre>', 
  array('@place_holder', print_r( $my_array, TRUE))
);


About Git - Git Basics - Become a git guru - Getting Started - Git Basics


About Version Control

What is “version control”, and why should you care? Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later.

Personally I love some links to study about Git. Listing those links below.

1) http://git-scm.com/doc - One of the wonderful kick start link to Git.

You will get  clear idea about Git from this link. Topic wise explanation is there as mentioned below.


2) http://git-scm.com/videos - Video links about Git.




Tuesday, January 27, 2015

Add a new configurations block to admin/config

We have frequent requirement to create admin form in Drupal.

Below is the simple example to create admin configuration block for your setting form.

$items['admin/config/myblk'] = array(
'title' => 'Title of the block',
'description' => 'description',
'position' => 'left',
'weight' => -25,
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('access administration pages'),
);
$items['admin/config/myblk/myblk-setting'] = array(
'title' => 'Link to settings',
'description' => 'Description about link',
'page callback' => 'drupal_get_form',
'page arguments' => array('call_your_setting_form'),
'access arguments' => array('administer authentication'),
'weight' => 10, );

Using coder.module to auto-format your code with drush

Coder  is a one of the best Drupal module to format / clean your Drupal codes.

Find more details about it here

After installed coder module, got to http://your-site/admin/config/development/coder

Select module / theme which is needs to be cleaned and click RUN REVIEWS.

You will get report as listed below.



you can automate this process by one line Drush command (drush coder-format ../path/to/your/module).

// download coder module
drush dl coder
// enable this module
drush en coder // drush en coder -y (Yes by default)
// auto format Or clean code
drush coder-format sites/all/modules/custom/your-module - path to your module

Note: Its cleaning inc,tpl,module,install files. Not cleaning js & css files.
Link to original post.

Friday, January 23, 2015

Export data to excel in php

Below is the simple script to exporting your data to Excel file.

You can download below codes from github.

function download_xls() {

  $data = array(
    array("Kali", "Sundar", 28),
    array("Amala", "silk", 18),
    array("Vinoth", "bharath", 31)
  );

  // Form header names.
  $header = array("Firstname", "Lastname", "Age");

  // File name for download.
  $file_name = "Export_data_" . date('Ymd') . ".xls";

  // This option to trigger download widget.
  header("Content-Disposition: attachment; filename=\"$file_name\"");
  header("Content-Type: application/vnd.ms-excel");

  // push header to file.
  echo implode("\t", $header) . "\n";

  foreach($data as $row) {
    // Push each row contents.
    echo implode("\t", $row) . "\n";
  }
  exit;
}

// Phpexcel is a drupal module to do export / create xls files.

Below is the example code to export xls file with out any open file format issue.

 module_load_include('inc', 'phpexcel');

// Create directory my_dir.
  $filepath = 'public://my_dir/';
  file_prepare_directory($filepath, FILE_CREATE_DIRECTORY);

  // Store file.
  $dir = file_stream_wrapper_get_instance_by_uri('public://my_dir')->realpath();
  $filename_timestamp = "file-name" . date('d-m-Y') . ".xls";
  $path = "$dir/$filename_timestamp";

  // Use the .xls format
  $options = array('format' => 'xls');
  $excel_result = phpexcel_export($header, $data, $path, $options);

  // To avoid file format issue while open file.
  $file = new stdClass();
  $file->uri = $path;

  header('Content-Type: application/vnd.ms-excel');
  header('Content-Disposition: attachment; filename="' . $filename_timestamp . '"');
  header('Expires: 0');
  header('Cache-Control: must-revalidate');
  header('Pragma: public');

  readfile($file->uri);
Usage in Drupal:

1) Create hyper link in your page <a href="download/xls"> Export </a>
2) Create menu call back for this URL.

Ex:
  $items['download/xls'] = array(
    'page callback' => 'download_xls',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );

Original post - See more details here [>>];

Wednesday, January 7, 2015

PHP sort multi-dimension array by value

Below is the example code for sorting multi-dimension array by its value.

$my_array = array (
  '0' => array (
      'ID' => 425,
      'ln' => 'CBolware',
      'fn' => 'Christian',
      'mi' => '',
     ),
  '1' => array (
      'ID' => 423,
      'ln' => 'Bernstein',
      'fn' => 'ZBear',
      'mi' => 'D.',
     ),
  '2' => array (
     'ID' => 419,
     'ln' => 'DBellweather',
     'fn' => 'Brent',
     'mi' => '',
    ),
  '3' => array (
     'ID' => 356,
     'ln' => 'ABayleaf, III',
     'fn' => 'Joe',
     'mi' => 'X.',
    ),
  '4 '=> array (
     'ID' => 336,
     'ln' => 'Public',
     'fn' => 'John',
     'mi' => 'Q.',
    ),
);

usort($my_array, function($a, $b) {
   return strcmp($a['fn'], $b['fn']);  // fn -> key of the array
});
print_r($my_array);

Monday, October 13, 2014

Drupal ajax form submit progress overlay

Use below code to show progress bar while submitting ajax form in Drupal.

/* ajax form submit throbber style */ 
.custom_wrap .ajax-progress .message,
.custom_wrap .ajax-progress-throbber .message{
  color: white;
  font-size: 20px;
  padding: 30px;
}
.custom_wrap .ajax-progress .throbber,
.custom_wrap .ajax-progress-throbber .throbber {
  float: none;
  margin: 250px auto 0 auto;
  width: 128px;
  height: 128px;
  background: url("../images/ajax-throbber.gif") no-repeat center center;
}
.custom_wrap .ajax-progress,
.custom_wrap .ajax-progress-throbber {
  width: 100%;
  height: 100%;
  background-color: #000;
 opacity: 0.6;
 filter: alpha(opacity=60);
  position: fixed;
  top: 0;
  left: 0;
  z-index: 500;
  vertical-align: middle;
  text-align: center;
}