Wednesday, November 25, 2015

Drupal preview image with remove option after upload file using form API

Preview image with remove option after upload file using form API.

// Create form with '#type' => 'managed_file'.
function kali_custom_form($form, &$form_state) {
 $form['upload'] = array(
  '#type' => 'managed_file',
  '#title' => t('Image Preview'),
  '#upload_location' => 'public://',
  '#theme' => 'kali_preview_theme', // define theme for preview
  '#progress_indicator' => 'throbber',
  '#upload_validators' => array(
  'file_validate_is_image' => array(),
 'file_validate_extensions' => array('jpg jpeg gif png'),
// Write hook_theme() as mentioned below.
function hook_theme() {
  return array(
   'kali_preview_theme' => array(
   'render element' => 'element',
   'file' => '', // you can write code in separate file.

// Include below function as mentioned in file or remove file include in hook_theme
and write below function in module file.
function theme_kali_preview_theme($variables) {
 $element = $variables['element'];
 $output = '';
 if($element['fid']['#value'] != 0 ) {
  $output .= '
'; $output .= theme('image_style', array('style_name' => 'thumbnail', 'path' => file_load($element['fid']['#value'])->uri, 'getsize' => FALSE)); $output .= drupal_render_children($element); $output .= '
'; } return $output; }

Some useful references:
-- Preview image after upload usinf FAPI.
-- Display uploaded image after upload.

Enable PHP OPcache in XAMPP windows

PHP 5.5.0 & later OPcache will be available by default.
Please add below code in php.ini to enable it.
 zend_extension = "C:\xampp\php\ext\php_opcache.dll"
 // Restart apache after enabled it.

opcache in xampp windows

Wednesday, November 4, 2015

Add group of fields using jquery or javascript

I had one requirement that adding some group of fields into some form.

Have used below set of HTML & jQuery codes.

Original posts:

Name Year Description
// jQuery
function checkRemove() {
    if ($('div.container').length == 1) {
    } else {
$(document).ready(function() {
    $('#add').click(function() {
    $('#remove').click(function() {

Below code to attach remove button against each group.
$(document).ready(function() {
    var removeButton = "";
    $('#add').click(function() {

    $('#remove').live('click', function() {

Tuesday, November 3, 2015

Drupal Queues

Using Drupal Queues, you can do so many bulk operation or batch process easily.

Shared some interesting links for further reading.

Original posts:

Javascript get array key value

Using below simple code, you can read Key & value from Java script array or object

var foo = { 'alpha' : 'puffin', 'beta' : 'beagle' },
keys = $.map(foo, function(v, i){
  return i;

Filter values from an array similar to SQL LIKE '%search%' using PHP

Array search or array filter similar to SQL LIKE '%search%' in PHP.

In one of my project, I need to display all the country names in select option as a drop down list.

Client expected filter option based on user input.
Ex: If user type "IN", we need to display country name starts with "IN".

Result like "INDIA", "INDONESIA", etc ..

Used below code to achieve it.
 $input = preg_quote('IN', '~');
 $result = preg_grep('~^' . $input . '~i', $list); // List is PHP array of values
// ~^ - to start from beginning.
// ~i - for case insensitive
Original post

Drupal custom pagination - override theme_pager

You can override Drupal pagination using below code.

I have faced below issue in one of my project.

ie, Actual total query result is different than displaying result.

Ex: My query total count was 100. But while displaying, based on requirement we need to display only 80 items. So when I use theme('pager') in template, I got result for 100 which is wrong. We need to display pager for only 80 items.

I have done below changes to override Drupal pagination.
// Menu callback function
function display_some_items() {
  // Query to fetch data with pager.
  $data['result'] = It contains filtered items, not equal with actual total count.
  $data['custom_pager'] = my_custom_pager($filtered_count); // custom pagination callback.
  $output .= theme('some_tpl_file', $data);

// custom pagination
function my_custom_pager($filtered_count = 0) {
  pager_default_initialize($filtered_counts, 10); // Pagination Limit 10
  $args = array('quantity' => 10,'tags' => array('<<','<','','>','>>'));
  return theme('pager', $args);

// Print below code in your template
print $custom_pager; // It will display pager based on filtered count.