Validating CCK Form Fields


Custom validations with form error set on CCK may require custom coding. This is one such exercise for both Drupal 6 and Drupal 7.

Drupal 6

* Validations for Employee Leave

function custom_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  if (
$op == 'validate' && $node->type == 'lms') {
$start_date =$node->field_lms_start_date[0]['value'];
$end_date =$node->field_lms_end_date[0]['value'];
$start_date > $end_date){
form_set_error('field_lms_start_date', 'Start Date of Leave Should be less than End Date of Leave');

Site Benchmarking and Performance Improvement

Improving Site Performance:
1. Disable all caching in the site and do benchmark for anon user
2. Benchmark for auth user for a particular page
3. Check logs to see if any notice or error is happening in the site. Solve the issue and notice BM
4. Disable unwanted modules if any and do another benchmark. Note the diff.
5. Modify or update apache or mysql setting minor if any to see noticiable change in benchmark
6. Optimize PHP and see if noticeable difference occurs
7. Install Devel and check queries and page rendering times

Programmatically creating/deleting/modifying field-collection

Enough Said , visit the link:

One change wrt to deleting Field Collections is as follows:

= node_load($node_id);
$field_collection_item_value = array();
foreach (
$node->field_collection_field[LANGUAGE_NONE] as $key => $value) {
$field_collection_item_value[] = $value['value'];
entity_delete_multiple('field_collection_item', array($field_collection_item_value));

Using Git


Version control of code files is important. Also important is to ensure that it is accessible throughout and also to the community from which we gain. I tried creating an account in GitHub, create and fork a repository and add files via commandline using git in Github.

Setting Up Git:

Module Specific styling and javascripts


Its not a good practice to add a module related theming details(css/jquery) in a /sites/all/themes/your_theme folder. In Drupal 7 one can use a module's .info file for including module-specific css/javascripts. Example given below:

name = Range Idicator
description = Indicates range values & out of range values in different color.
core = 7.x
package = "Jagriti Innovations"
stylesheets[all][] = ji_indicator.css
dependencies[] = custom_formatters

Debug you PHP code using DRUSH


Drush is a very handy tool which makes development in Drupal very fast and easy. Almost everyting is JACA("just a command away"). This particualr tip will help you to evaluate your php code using Drush.


Consider a function: ji_custom($node). To evaluate/debug this function I type following in Drush:

drush php-eval "ji_custom($node);"

Aliases for php-eval are: eval, ev

N.B: JACA is invented while writing this post :)

Provide a link to add content from VIEWS

I found out a better way to provide "Add content" link at the footer region of a VIEW. We can achieve followings:

- Pass any number of arguments.
- User can access link only if he/she has permission.

N.B- use Entity Reference for prepopulating valued from URL


global $user;
$view = views_get_current_view();
$args = $view->args[0];
if (user_access('create test_report content', $user)) {
$output=l(t('Add new Biochemistry Test '), 'node/add/test-report', array(
'query' => array(
'field_tr_patient_ref' =>$args,

Batch Process Images in Linux

Sometimes its a pain when you want to modify, resize multiple files at once in Linux. Fortunately ImageMagick comes with a handy command called convert that makes the work simpler

Install ImageMagick in Ubuntu

sudo apt-get install imagemagick

Batch Process set of files in a folder and resize them to 1024X768

for file in *.jpg; do convert "$file" -resize 1024x768 "$file"; done

You may also try an app called as phatch for GUI related batch processing of images.

Snippet to bulk rename files by replacing strings in filename