Archive for January, 2014

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

How to disable views programmatically when the offering module is disabled

Here is a snippet of code you would put in mymodule.install

* Disable workbench views
function mymodule_update_7001() {
// a list of views (their view name) I want to disable
$viewnames = array(
// grab list of views that are already disabled
$views_status = variable_get(‘views_defaults’, array());
// add our views to be disabled to the list
foreach ($viewnames as $key => $viewname) {
$views_status[$viewname] = TRUE;
// reset the variable with the new list
variable_set(‘views_defaults’, $views_status);
// empty cache now
if (function_exists(‘views_invalidate_cache’)) {

Leave a comment

How To Disable Custom Panel Page Programmatically?

You can automatically disabled custom panel page  via hook_update_N.  After little digging around in CTools module codebase, I ended up with the following code snippet.

$page = page_manager_get_page_cache('<page_name>'); // Replace <page_name> 
$function = ctools_plugin_get_function($page->subtask, 'enable callback');
$result = $function($page, TRUE);