Rules cron based condition code for allowing execution only once

Technologies: 

<?php

/*
* Part of Rules condition code that returns true if the current time during cron is between 6 am and 7 am
*/
$ji_daily_status = variable_get("ji_daily_report","1"); //Check if the variable is set, if not set it to 1 [First Time]

/*
* This fragment checks if the time is between 6 and 7, if so enables the flag
* If any other time, sets the ji_daily_report so that next time it executes when cron runs between 6 and 7
*/
$time = date('H'); // Get the current time
/* Any arbitrary time - Checks if the time is not between 6 am and 7 am */
if($time < 6 || $time > 7){

Drupal Features Update and Revert

Features Update: Update a feature module on your site. This updates the feature module in the site if the components are overriden

drush fu <feature-module-name>

Features Revert: Revert a feature module on your site. Reverts the basic configuration in the module onto the site removing any custom configurations made at the database end.

drush fr <feature-module-name>

Custom Template files for Drupal CCK Forms

To render a Drupal CCK form in a template file, add the following lines in the currently enabled theme. Assuming the enabled theme to be footheme

For a cck form called external_exam, the theme function can be used to provide a template file called external_exam_form.tpl.php under templates directory.

<?php
function footheme_theme() {
  return array(
   
'external_exam_node_form' => array(
     
'arguments' => array('form' => NULL),
     
'template' => 'templates/external_exam_form',
     
'render element' => 'form'
   
),
  );
}
?>

Rendering a CCK field programmatically

Sometimes we need to render a field when we have a node object containing field values.

field_view_value is a Drupal API that allows us to render any field. Example: Display the Invoice Description Field in a Node Object. The display would take care of any input filter that is applied in the field

<?php
$invoice_description
= field_get_items('node', $node, 'field_invoice_description');
print
render(field_view_value('node', $node, 'field_invoice_description',$invoice_description[0]));
?>

The other way to display the label and data completely rendered by Drupal.

<?php

Find the Largest Table in a MySQL Database


SELECT CONCAT(table_schema, '.', table_name),
CONCAT(ROUND(table_rows / 1000000, 2), 'M') rows,
CONCAT(ROUND(data_length / ( 1024 * 1024 * 1024 ), 2), 'G') DATA,
CONCAT(ROUND(index_length / ( 1024 * 1024 * 1024 ), 2), 'G') idx,
CONCAT(ROUND(( data_length + index_length ) / ( 1024 * 1024 * 1024 ), 2), 'G') total_size,
ROUND(index_length / data_length, 2) idxfrac
FROM information_schema.TABLES
WHERE table_schema='database_name'

Memcache For Drupal in Ubuntu 13.04

Installation and Configuration of Memcached Daemon and PECL Memcache in Ubuntu for Drupal 7

Steps:

1. Install memcached in the server

sudo apt-get install memcached libmemcached-tools
sudo apt-get install php5-dev php-pear make
sudo pecl install memcache

Latest dev Version of Memcache can be installed via.

# If you haven't already installed PECL memcache
pecl install memcache-beta
# If you wish to "upgrade" to a beta release
pecl upgrade memcache-beta

2. Add memcache.ini in PHP conf.d directory

Handy Linux Commands

Technologies: 

Split: Split a text file in half (or any percentage) on Ubuntu Linux

The following command splits access.log files into multiple files with 6000 lines each

split -l 6000 access.log

Split files into size of 5M each

split --bytes 5M --numeric-suffixes --suffix-length=3 foo.mysql  last_part.mysql

Diff : Get Difference between files and folders
Following command provides recursive difference