PHP

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

Displaying Graph Using HighCharts

HighCharts is a charting library tool made freely available for non commercial usage. Drupal HighCharts 2.x has no other module dependency except that HighCharts library needs to be downloaded from here. There are a huge variety of options made available in HighCharts API which can be used for manipulation and for additional info.

The example below is a Line Chart uses Drupal HighCharts version 2.x

<?php
/* Render Line Charts using Drupal Highcharts 2.x */
function jicustom_chart_render($chart_arr){

$options = new stdClass();

Using Drush Alias

drush aliases allow you to run a drush commands on your local server but actually execute the command on a remote server.

One can create aliases in a folder that drush recognizes, mostly inside the .drush folder for Linux Based Systems. For a mysite example, we can create a file called as mysite.aliases.drushrc.php inside the .drush folder (Or any folder which Drush goes through before executing). All aliases in a site can also be combined in a single file called as aliases.drushrc.php

Example data that can be added to mysite.aliases.drushrc.php

<?php

Validating CCK Form Fields

Technologies: 

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

<?php
/**
* 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'];
    if(
$start_date > $end_date){
     
form_set_error('field_lms_start_date', 'Start Date of Leave Should be less than End Date of Leave');
    }
  }
}
?>

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,

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'];