Coding

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

Programmatically creating/deleting/modifying field-collection

Enough Said , visit the link: http://rajanmayekar.com/blog/programmatically-creating-deleting-modifyin...

One change wrt to deleting Field Collections is as follows:

<?php
$node
= node_load($node_id);
$field_collection_item_value = array();
foreach (
$node->field_collection_field[LANGUAGE_NONE] as $key => $value) {
   
$field_collection_item_value[] = $value['value'];
}
entity_delete_multiple('field_collection_item', array($field_collection_item_value));
?>

Module Specific styling and javascripts

Technologies: 

Its not a good practice to add a module related theming details(css/jquery) in a /sites/all/themes/your_theme folder. In Drupal 7 one can use a module's .info file for including module-specific css/javascripts. Example given below:
Filename: ji_indicator.info

name = Range Idicator
description = Indicates range values & out of range values in different color.
core = 7.x
package = "Jagriti Innovations"
stylesheets[all][] = ji_indicator.css
dependencies[] = custom_formatters

Update File Path in Drupal

We have a site that we had been migrating since Drupal 4. Few days back when we tried to migrate the same from Drupal 6 to Drupal 7. There was an issue with respect to file location that was present since previous upgrades. To resolve that a symlink was created called "files" with linked to sites/default/files during D6 upgrade. This led to first loss of many files as Drupal does not handle symlink based file saving well. Secondly the files created were saved under public://files/filename.ext instead of public:://filename.ext which caused created files not being accessible.

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

Utility Methods

Technologies: 

function_exists: function_exists — Return TRUE if the given function has been defined

Syntax:

bool function_exists ( string $function_name )

Example:

<?php
if (function_exists('payroll_getcomponents')) {
    echo(
"payroll_getcomponents functions is available");
}else{
    echo(
"payroll_getcomponents functions is NOT available");
}
?>

profile2_load_by_user: Loads the profile data of a user. Takes user id as input

Syntax:

profile2_load_by_user($account, $type_name = NULL)

Example:
<?php

How to Set Filters for a View in Drupal 7 programmatically

Technologies: 

Accessing a view programmatically

<?php
$view = views_get_view('view_name');
$view->init();
$view->set_display('default'); // or display id like page_1, block_1
$view->set_arguments(array($ARGS));
//display_name = 'default' or 'page' and so on
//filter_name is the name of the filter
//debug($view->display['display_name']->handler->options['filters']['filter_name']);
$view->display['default']->handler->options['filters']['date_filter']['value']['min']='2012-05-01';
$view->display['default']->handler->options['filters']['date_filter']['value']['max']='2012-06-30';

Creating a Drupal 7 Node Programmatically


<?php
/**
* Basic Node Creation Example for Drupal 7
*
* This example:
* - Assumes a standard Drupal 7 installation
* - Does not verify that the field values are correct
*/
$body_text = 'This is the body text I want entered with the node.';

$node = new stdClass();
$node->type = 'article';
node_object_prepare($node); //Creates default settings for the node of a type
$node->title = 'Node Created Programmatically on ' . date('c');
$node->language = LANGUAGE_NONE; //String constant value for "und"

/* If u have body text do the following below

Update Comment Status of a Node

Just recently I had the issue where a list of nodes imported via user import as a content profile had comment status disabled.

There were thousand of nodes to update, hence the best way was to use VBO. However, there was no option provided for comment status update, hence the alternative was to execute "Arbitrary PHP Code".

The following snippet below can be used for updating node status for nodes selected in VBO:

Add Tab - A Simple Drupal Module

Technologies: 

addtab is a simple Drupal Module that adds a tab that provides links for adding same content type, ie whenever a content is viewed a tab would appear that would link to creating similar content.

The Create tab that you can see in this page being displayed is basically due to this module.

A drupal module consists primarily of two files .info and .module file.

addtab.info:

; $Id$ addtab.info,v 1.0 2011/02/20 01:26:00 amitsedai Exp $
name = addtab
description = Creates a new tab in a node view for adding same content type
core = 6.x