Skip to main content

Drupal 7

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

Displaying Errors in Drupal

Submitted by amitsedai on
During testing phase of the system, there are WSOD for whom no logs are available. Devel is one option, but for knowing just what might be the reason, the following changes in the settings.php file will reveal any error that occured. <?php error_reporting(E_ALL); ini_set('display_errors', TRUE); ini_set('display_startup_errors', TRUE); ?>

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

Memcache For Drupal in Ubuntu 13.04

Submitted by amitsedai on
Installation and Configuration of Memcached Daemon and PECL Memcache in Ubuntu for Drupal 7 Steps: 1. Install memcached in the server sudo apt-get install memcached libmemcached-tools
sudo apt-get install php5-dev php-pear make
sudo pecl install memcache

Latest dev Version of Memcache can be installed via. # If you haven't already installed PECL memcache
pecl install memcache-beta
# If you wish to "upgrade" to a beta release
pecl upgrade memcache-beta

2. Add memcache.ini in PHP conf.d directory

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

jQuery Mobile Page Load Issue

Submitted by rajat on
Sometimes when we visit a link, specially on mobile, there is a situation when we place a link and reaching a page clicking that link leads to a situation where the jQuery library is not loaded into the browser. This could affect conditional fields and other jQuery widgets. The solution is to add rel="external" to the link.
-- Source http://stackoverflow.com/questions/10389751/jquery-mobile-page-load-issue

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

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.