Module Specific styling and javascripts

Technologies: 

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:
Filename: ji_indicator.info

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

Technologies: 

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.

Examples:

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

Example:

<?php
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

Update File Path in Drupal

We have a site that we had been migrating since Drupal 4. Few days back when we tried to migrate the same from Drupal 6 to Drupal 7. There was an issue with respect to file location that was present since previous upgrades. To resolve that a symlink was created called "files" with linked to sites/default/files during D6 upgrade. This led to first loss of many files as Drupal does not handle symlink based file saving well. Secondly the files created were saved under public://files/filename.ext instead of public:://filename.ext which caused created files not being accessible.

Saving Form Selection using Form API

During custom form submission we would want to remember the previous selection. There are many ways to do that, which includes using Sessions and variable_set to set the default value but this is an overkill if you wanted to simply preserve/remember the selection in the next page or after submission. The best way is to use $form_state['storage'] in your submit handler to hoard values between steps.

Consider an example:
<?php
function custom_form_submit($form, &$form_state) {
$start_date = $form_state['values']['startdate'];
$end_date = $form_state['values']['enddate'];

Executing PHP in command line

Technologies: 

There are times when one needs to execute PHP in command like, for example trying the date command output or a one line execution. For trivial tasks, executing the same at the command line without the need for writing and executing at a file level is a great tool for time saving.

The argument to be used is : -r

Example:

php -r '
$a = range(0,2);
foreach($a as $b) {
   echo "entry: $b\n";
}

The output is:
entry: 0
entry: 1
entry: 2

Another example:
php -r '
> echo date("d-m-Y");
> echo "\n";
> '
14-01-2013

Add www to URL using Apache mod_rewrite

For sites belonging to a domain name(Not Subdomain), one can access the site using the domain name with www, Ex: www.example.com or without it: example.com

Both ways the site can be accessed when a ServerAlias is created in the config. However this is confusing as session information for both is different, which means if one is accessing the site with www preceded and logs in, on accessing the site without www preceded - the server would have no information saved.

To redirect any domain name to use www by default, the following mod_rewrite condition can be used:

Executing Views Bulk Operations Using Drush

Many times we use Views Buk Operations with Batch Api for doing a big set of operations. When the count of operations is huge, even Batch API gets short. A faster and more efficient way to do that is using drush to do the heavy backlifting.

The commands are:

vbo-execute      Execute a bulk operation based on a Views Bulk Operations (VBO) view.                                                   
vbo-list             List all Views Bulk Operations (VBO) views, along with the operations associated with each.

Examples: