Skip to main content

Coding

Listing and Checking Drupal Permissions through Code

Submitted by rajat on
The following lists all the permissions available in Drupal. <?php // Render role/permission overview: $options = array(); foreach (module_list(FALSE, FALSE, TRUE) as $module) { print_r($module); // Drupal 6 // if ($permissions = module_invoke($module, 'perm')) { // print_r($permissions); // } // Drupal 7 if ($permissions = module_invoke($module, 'permission')) { print_r($permissions); } } ?> You could check whether a particular permission is set or not using the following: <?php user_access($string, $account = NULL) ?>

Rules cron based condition code for allowing execution only once

Submitted by amitsedai on
<?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

Submitted by amitsedai on
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

Submitted by amitsedai on
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

Programmatically Saving File

Submitted by amitsedai on
Get the filename URI path from the dir_name located under public files directory. Save data in the destination. If file exists with the name, replace Get the download link. <?php $dest = file_build_uri('dir_name/'.$filename); $file = file_save_data($data, $dest, FILE_EXISTS_REPLACE); $download_link = file_create_url($file->uri); ?>

Displaying Graph Using HighCharts

Submitted by amitsedai on
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

Submitted by amitsedai on
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

Submitted by amitsedai on
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']); }

Drupal Date Manipulation

Submitted by amitsedai on
An easier way of getting difference between dates is achieved using the DateObject provided by Drupal. <?php $now_date = date_now(); $last_donation_date = new DateObject($node->field_date[LANGUAGE_NONE][0]['value'], 'UTC'); // Get difference in days in positive if $now_date is greater than $last_donation_date $diff_days = $last_donation_date->difference($now_date, 'days', FALSE); ?> Print a Date Object in any format using date_format_date: <?php print date_format_date($date_object, 'custom', 'Y m d'); ?> -- Source http://drupalcontrib.org/api/drupal/contr

Get All Options of a CCK Select Field

Submitted by amitsedai on
If you have a CCK Select field and you would want to extract the various options available in that field via custom code, the following lines help you achieve that. <?php $field = field_info_field('field_name'); $allowed_values = list_allowed_values($field); ?> Get the field name of the field(Machine Name) and list all possible options.