Archive for category PHP

SOLVED!! How can you make form_set_error() affect the only form submitted?

Drupal form_set_error() function provide validation message based field name. So if you have same field name in same page , after the submission you can see both fields are marked as red color(Error class). It validate or show message for right form but it provide error class for all element which are same.

I found a way to solve it. If you render those form using drupal_get_form then you can add one extra line in top to get rid of this problem.

for example :-

drupal_static_reset('form_set_error'); // adding this line makes you life out of hell.-)
$form = drupal_get_form('webform_client_form', $node, $submission);
return drupal_render($form);



Leave a comment

Saving node’s fields without saving the node itself

Drupal core function node_save() is used to save nodes and it is frequently used to programmatically alter and save nodes’ data, which includes fields (aka CCK fields) as well. If you want to update a field of a node you could do it like this,

$node = node_load($nid);
$node->field_fieldname[LANGUAGE_NONE][0][‘value’] = ‘some value’;

However, there may be a case when you might want to save a node’s field, but not the node itself. For example, you may not want to update the node’s timestamp for the last update, because you want it to be changed by human user activity only. In another case, you may not want to call node_save() and thus invoke chain reactions such as email notifications when the node is updated. These two cases are what actually happened to me.

A Drupal function field_attach_update() may save your day if you encountered a similar problem that I had. You could actually replace node_save() with field_attach_update(),

$node = node_load($nid);
$node->field_fieldname[LANGUAGE_NONE][0][‘value’] = ‘some value’;
field_attach_update(‘node’, $node);


Leave a comment

Floating a textfield next to a radio button in Drupal form

This can actually be done entirely with the Forms API. Here is an example:


// This will mimic the normal form element title as well as provide the group for our radio options.
$form[‘nations’] = array(
‘#type’ => ‘item’,
‘#title’ => t(‘Nationality’),
‘#description’ => t(‘<Enter your field description here.>’)
$form[‘nations’][‘american’] = array(
‘#type’ => ‘radio’,
‘#title’ => t(‘American’),
‘#default_value’ => 1,                    // If there is a default value, this should also be specified for each radio button.
‘#return_value’ => 1,
‘#parents’ => array(‘nations’),           // You must specify this for each radio button for them to act as a group.
‘#prefix’ => ‘<div>’  // The first radio button needs to make the style match a normal radios group.
$form[‘nations’][‘german’] = array(
‘#type’ => ‘radio’,
‘#title’ => t(‘German’),
‘#return_value’ => 2,
‘#default_value’ => 1,
‘#parents’ => array(‘nations’)
$form[‘nations’][‘other’] = array(
// The ‘container-inline’ class places elements next to each other, while the ‘form-item’ class provides the correct spacing between options.
‘#prefix’ => ‘<div>’,
‘#suffix’ => ‘</div>’
// By supplying the title here, instead of using the ‘#field_prefix’ property of the textfield, clicking the text will also select the radio button.
$form[‘nations’][‘other’][‘other_option’] = array(
‘#type’ => ‘radio’,
‘#title’ => t(‘Other:’),
‘#return_value’ => 3,
‘#default_value’ => 1,
‘#parents’ => array(‘nations’)
$form[‘nations’][‘other’][‘other_textfield’] = array(
‘#type’ => ‘textfield’,
‘#default_value’ => ”,
‘#size’ => 20,         // The default size is a bit large…
‘#suffix’ => ‘</div>’  // End of the “form-radios” style.

In your validation and/or submit callbacks, the selected radio option will then be in $form_state['values']['nations'] and the textfield value will be in $form_state['values']['other_textfield'].

With a little more work, you can change the example so that you don’t have to create each element manually .-)

, ,

Leave a comment

Crap Newsletter template in Outlook 2007/2010 in Drupal


I used Drupal simplenews to send newsletter to all the memebers of my site.  It was a real hell of life. It takes thousands of my hours to spend to fix the template but I cant. because it was not a probelm

in simplenews. I  used mimemail which create all those problem for me. Mimemail add the site css file in mail body autometically and it does not work that inline css of newsletter in Outlook when you sending newsletter from

Drupal system.

Then i found a solution which guide me in the  below link:

All is that I need to override the mimemail message function. You need to remove the Css file to added in variable field. Then try with it and see it works .-)


function YourThemeName_preprocess_mimemail_message(&$variables)
// otherwise you’ll get random css added to the newsletter
$variables[‘css’] = “”;

, ,

Leave a comment

Contain new line in field when exporting CSV file in php


It is really strange problem. There is a new line in my address field and when i am adding that value to download in CSV file , all column messed up.

I tried to use nl2br , str_replace , encoding function to find out solution of that mess. But nothing work.

Then I talk with one GURU (Boss 🙂 ) , he told me you need to use ” in place of  ‘  because PHP cant replace new line if it is quoted by single ‘.

I was just surprised,  then i used the function

str_replace(array(“\r\n”, “\n\r”,”\n”,”\r”),” “,$string);

It just work in one sec 🙂

So, I want to share this information to all my PHP developers because many of them might not know so as I . 🙂



Leave a comment