Coding

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){

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();

Drupal Custom Flag Operations

Technologies: 

Create a flag link for a user:

<?php
print flag_create_link('user_flagname', $user_uid);
?>

Get Flag of a Type and see if it is flagged for a node and not the number of times it got flagged.

<?php
$flag
= flag_get_flag('bookmarks') or die('no "bookmarks" flag defined');
if (
$flag->is_flagged($node->nid)) {
  print
"This node is bookmarked!";
}
print
"The number of people who voted for this proposal:";
print
$flag->get_count($node->nid);  //
?>

Flagging or Unflagging an item:
You use the $flag->flag() method to either flag or unflag an item. Example:

<?php

How to Modify Output of a View

Using views_pre_render hook one can alter the output of a view

<?php
/**
* Implements hook_views_pre_render().
*/
function jicustom_views_pre_render(&$view) {
if($view->name=='stock_report'){
$results = $view->result;
foreach ($results as $key => $result) {
$collection_name = $result->field_collection_item_field_name;
if($collection_name == 'field_transaction_items_outgoing'){
//Modify output
$results[$key]->field_field_trans_item_quantity[0]['rendered']['#markup'] = -($result->field_field_trans_item_quantity[0]['raw']['value']);
}