Skip to main content

Drupal API

Validating CCK Form Fields

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

Submitted by amitsedai on
Enough Said , visit the link: http://rajanmayekar.com/blog/programmatically-creating-deleting-modifying-field-collection-item-node 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)); ?>

Provide a link to add content from VIEWS

Submitted by sunildhimal on
I found out a better way to provide "Add content" link at the footer region of a VIEW. We can achieve followings: - Pass any number of arguments. - User can access link only if he/she has permission. N.B- use Entity Reference for prepopulating valued from URL Example: <?php
global $user;
$view = views_get_current_view();
$args = $view->args[0];
if (user_access('create test_report content', $user)) {
$output=l(t('Add new Biochemistry Test '), 'node/add/test-report', array(
'query' => array(
'field_tr_patient_ref' =>$args,

Saving Form Selection using Form API

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

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

Creating a Drupal 7 Node Programmatically

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

Submitted by amitsedai on
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: <?php $object->comment = COMMENT_NODE_READ_WRITE; node_save($object); ?> Happy Drupaling! -- External Links: http://drupal.org/node/673828