Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I have some nodes of a content type with following fields:

  1. title
  2. body
  3. image

I want to programmatically (!important) set the body field as empty (to make it like it was never set with any value).

This is what I have done:

$this_node = node_load($nid);
$this_node->body = array(); //to set it as an empty field
node_save($this_node);

After executing this, the node is not saved. Instead, in db_log following error is loged:

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '104418'
for key 'PRIMARY': INSERT INTO {node} (nid, vid, type, language, title, uid, status, created,
changed, comment, promote, sticky, tnid, translate, uuid) VALUES
(*******placeholders & their values*******) in drupal_write_record()
(line 7170 of /var/www/rotary.local/docroot/includes/common.inc).

Is there someting I am missing? How I can set the body field as empty?

NOTE:

I don't want to use field_attach_update for caching purposes.

share|improve this question
2  
Perhaps this would work = $this_node->body[$this_node->language] = array(); –  Krister Andersson Feb 17 at 10:05
    
@KristerAndersson That's spot on, you should put it in as an answer –  Clive Feb 17 at 10:10
    
@KristerAndersson: It does work. But when I save the content originally with no body field then $node array shows $node->body as an empty array. However, using the logic you suggested, it shows $node->body as an array with $node->body[language] as empty array. I am worried this may not be ideal. Could you please support your answer with something that can convince me that it is safe to do so. –  kmdhrm Feb 17 at 10:11
    
@Clive: Why does saving $this_node->body as empty array gives the PDOException error, while saving $this_node->body[$this_node->language] as empty array does not give the error –  kmdhrm Feb 17 at 10:14
    
Because the first method is incorrect, and the second method is correct :) Basically with the first method you're not providing a language code, which is necessary for the field API, and incorrect data is getting into the database. Next time you try to do the same thing, integrity constraints fail, and you get the error. –  Clive Feb 17 at 10:19

2 Answers 2

up vote 4 down vote accepted

To remove the value from you field you should do like this before saving it:

$this_node->body[$this_node->language] = array();
share|improve this answer
    
After testing the code with your suggestion I found that it still gives the error. However I am able to save the node using field_attach_update. What could be causing this? –  kmdhrm Feb 17 at 10:31
    
@DipakYadav - That seems very strange, what error do you get? –  Krister Andersson Feb 17 at 10:57
    
The error was being caused because of a whole lot different reason. Please see the comments to the question. –  kmdhrm Feb 17 at 11:36

To make body field empty you should implement hook_node_presave() and inside this, use-

   $node->body[LANGUAGE_NONE][0]['value'] = '';
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.