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.

Tuesday, May 26, 2015

Change Browser URL & Title without reloading (refreshing) page

Change browser URL & Title with out refreshing / reload the page.

Please see below link for more details.

Thursday, May 21, 2015

Drupal template redirect

I have faced one challenge recently where we need to redirect from one custom node template to another one template.
  • Created one template for custom content type (nature).
  • node--nature.tpl.php
  • node--my_nature.tpl.php (if your content type - my_nature: some time you will face this situation.)
  • This content type has one taxonomy reference.
  • For particular taxonomy reference(condition), we need to display different appearance (layout).
  • So done below fix to redirect for different tpl.
When ever you visit nature content, you will be landed on node--nature.tpl.php.
Inside this tpl, I have used below condition to use different tpl file.

// Write below code inside node--natute.tpl.php on top of the file.
if (condition) {
 print theme('use_different_layout', array('node' => $node)); // pass argument if you need.
// Create different tpl file for "use_different_layout" theme by using hook_theme().

hook_theme() example.
function hook_theme() {
return array(
    'use_different_layout' => array(
      'variables' => array('node' => array()),
      'template' => 'templates/different-layout.tpl.php',