How To Insert and Update Only Specific Fields Of Your Entity In Drupal 7

22 Oct · by Tim Kamanin · 2 min read

Time from time you need to update single or several specific fields of your entity in Drupal. It can be node, comment, taxonomy or some other custom entity. So how do we usually do this? Let's see:

$node = node_load(1); // 1 is node id we want to load

// Change field value
$node->field_product_description[$node->language][0]['value'] = 'This is a very nice and useful product. Buy it now! We want your money!';

// Save updated node
node_save($node);

This approach is totally ok, but hey, your poor Drupal did a massive job to update just one field: it loaded the whole node into memory and then, saved every field (even which wasn't updated) one by one into database. If your nodes are pretty big, it can produce a lot of unnecessary queries.

Here's how to update entity field in a more "diet" way by using Drupal 7 Field API, this is a general code template:

// Get the numeric id of your field by passing field name
$info = field_info_field('field_name_goes_here');
$fields = array($info['id']);

// Execute the storage function
field_sql_storage_field_storage_write('entity_type', $entity, 'update', $fields);

// Clear field cache
cache_clear_all("field:$entity->type:$entity->id", 'cache_field');

And here's how to use it on practice:

$node = node_load(1); // 1 is node id we want to load

// Change field value
$node->field_product_description[$node->language][0]['value'] = 'This is a very nice and useful product. Buy it now! We want your money!';

// Get the numeric id of your field by passing field name
$info = field_info_field('field_product_description');
$fields = array($info['id']);

// Execute the storage function
field_sql_storage_field_storage_write('node', $node, 'update', $fields);

// Clear field cache
cache_clear_all("field:node:$node->nid", 'cache_field');

So now, we updated only one field of the node without updating the whole node. Please note, that you can pass as many fields as you like via $fields array, so you can update more than one entity field at once.

That's all for now, thanks for reading this. Share with others!

A VERY BIG NOTE: Using this technique you can 't rely on drupal hooks system. No hooks are being invoked, updated nodes won't be queued for search indexing if you won't do this manually. Be awared.

Comments

Required for comment verification



Johan Haest

The cache stuff saved my day!

Reply · 5 years, 1 month ago
berimbolo

I've been searching for ages for this!!! Thank you so much for this post!

Reply · 5 years, 1 month ago
Juan

Awesome!!! just what i was looking for, will this work if i use this instead function hook_field_storage_write ?????

Reply · 5 years, 1 month ago
Yuriy Babenko

Nice tip, thanks!

Reply · 5 years, 1 month ago
Netdream

Good tip, but be careful! I suspect that no hooks are executed in this way...

Reply · 5 years, 1 month ago
Dalin

This article needs a BIG caveat that this is pretty much on-par with saving values directly to the DB. You'll manually need to clear caches, queue the node for search indexing, and anything else that is normally accomplished by the hook system.

Reply · 5 years, 1 month ago