• RESUME
  • LOGIN

balanced scale media

  • blog
  • projects
  • services
  • contact
Home › Blogs › boz's blog

Update CCK Fields In Custom Drupal Nodes

boz — Tue, 06/22/2010 - 13:17

This Post Has Been Updated From The Original : See Bottom

This was a major hang up on my recent project. I thought I would share in hopes of saving someone else sometime. The explanation for the code being this tedious has to do with CCK's database structure. Each field is given its own table, so to avoid costly queries CCK has a database cache. Here is the code. I had to read through several other posts to find this. I'll try to link to those when I get time.

// Load node to edit
$node = node_load($your_node_id);

// Index value of the cck field
$index = 0;
 
// value that you want to insert
$new_value = 3;
	
// Set value
$node->field_your_field[$index] = array('value' => $new_value);

// Save Node
node_save($node);

// Update content if node exists, otherwise use content_insert()
content_update($node);

// Clear CCK data cache so the new value will be loaded for dispaly purposes
db_query("DELETE FROM {cache_content} WHERE cid = '%s'", 'content:' . $node->nid . ':' . $node->vid);

Update:

Since writing this new things have come to light. While the code above works. It is overkill simply saving a CCK field. Also, there are more than one scenario for updating.

Scenario 1: node_save

// Load node to edit
$node = node_load($your_node_id);

// Index value of the cck field (Depends on the number of values your field accepts)
$index = 0;
	
// Set value
$node->field_your_field[$index] = array('value' => $new_value);

// Save Node
node_save($node);

node_save runs cache_clear_all() so you don't need to delete a row from CCK cache.

Scenario 2: db_query

// Load node to edit
$node = node_load($your_node_id);

// Index value of the cck field (Depends on the number of values your field accepts)
$index = 0;

// Save Node
db_query('UPDATE {content_type_x} SET field_y="%s" WHERE vid="%s" AND nid="%s"', $new_value, $node->vid, $node->nid);

// Clear CCK data cache so the new value will be loaded for dispaly purposes
db_query("DELETE FROM {cache_content} WHERE cid = '%s'", 'content:' . $node->nid . ':' . $node->vid);
  • Drupal
  • boz's blog
  • Add new comment

Thanks for sharing. I'd also

Anonymous — Fri, 01/27/2012 - 14:30
Thanks for sharing. I'd also add that:
  • node_save() invokes hook_nodeapi, updates node->changed, etc. which you may or may not want in your custom implementation.
  • In your simpler scenario 2, looks like the $index line is unused
-Bronius
  • reply

Why are you using node_submit

Anonymous — Sat, 03/12/2011 - 13:02
Why are you using node_submit if you load an existing node? It breaks the $node->created date.
  • reply

Good to know. For my

Anonymous — Sat, 03/12/2011 - 17:07
Good to know. For my purposes, this worked fine. What is your solution?
  • reply

Simply omit this line: $node

Anonymous — Mon, 03/14/2011 - 07:20
Simply omit this line: $node = node_submit($node); node_submit is only needed when you are building a new node object.
  • reply

Thanks!

Anonymous — Wed, 09/29/2010 - 12:57
Thanks for sharing! I was about to just code the SQL to update the content record for my content_type and did a quick google search 'drupal update cck field' and your post was on page 1 and the first one I checked. Worked perfect. - blainelang (d.o member) Nextide Inc.
  • reply

Recent Posts

  • Generate Word Documents With PHP
  • Delete CCK Inactive Fields
  • Delete Nodes From A Certain Date in Drupal
  • Automated Node Titles Without Auto Node Title
  • Drupal Camp Iowa Ajax Example
  • Get Paid To Learn Drupal!
  • Presenting "JavaScript Guidelines : When and When Not to Script"
  • Drupal Form Alter on a Node Reference Widget
  • The Paradox Of Drupal
  • Programmatically Creating Users in Drupal with Profile Fields
more

Topics

  • Android
  • Apace
  • Aptana
  • Bazaar
  • Browser
  • Code
  • Drupal
  • Eclipse
  • Flex
  • HTML 5

Archive

  • February 2008 (1)
  • May 2008 (1)
  • July 2008 (3)
  • December 2008 (2)
  • January 2009 (2)
  • March 2009 (3)
  • April 2009 (5)
  • May 2009 (1)
  • July 2009 (1)
  • August 2009 (3)
more

Links

 

  • blog
  • projects
  • services
  • contact

© 2012 Balanced Scale Media, LLC. All Rights Reserved.