Skip to main content

Drupal API

How to enable Ajax for CCK Fields in Drupal 7

Submitted by amitsedai on
The idea is to modify another CCK field based on selection of an item via a select list in another field. Let us consider a content type called article containing 2 additional fields: article_test_entity - which is an entity reference that displays node titles of all nodes and article_test_entity_type - which is a select list that contains Content Type names list with its key as the actual machine name of the content type. The idea is to restrict the list of node titles displayed on article_test_entity based on the content type selected via article_test_entity_type. <?php /*

Drupal Features Update and Revert

Submitted by amitsedai on
Features Update: Update a feature module on your site. This updates the feature module in the site if the components are overriden drush fu
Features Revert: Revert a feature module on your site. Reverts the basic configuration in the module onto the site removing any custom configurations made at the database end. drush fr

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.