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,

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

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

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

Advertisements

,

  1. Leave a comment

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: