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.

My articles have a field 'Ranking' and I'd like to give a value to this field after a node has been created. The value depends on the node's ranking in a view called 'Top 3'. I tried the following in my custom module 'Top', but it doesn't work.

function top_node_insert($node) {
  $top = views_get_view_result('top_3');

  $found = false;  
  foreach($top as $key => $value) {
    if ($value->nid == $node->nid) {
        $found = true;
        break;
    }
  }

  if ($found) $ranking = $key + 1;
  $node->field_hoogste_ranking[LANGUAGE_NONE][0]['value'] = $ranking;
}

What did I do wrong and what's the correct version?

share|improve this question
    
AFAIK, there is no hook_node_save. If you wish to do something after a node has been created, use hook_node_insert. –  AjitS Feb 1 at 18:32
    
@AjitS: I've tried the same code with hook_node_insert, but it doesn't work either. –  Jeroen Feb 1 at 20:01
add comment

2 Answers 2

up vote 4 down vote accepted

If you look at the Node API Hooks description here, you will see that hook_node_insert is called after field_attach_insert. That means that at the point hook_node_insert is called the fields for the node are already written to the database. If you keep your code in top_node_insert, then add field_attach_update('node', $node) to update the fields with the new data. It might look something like this:

function top_node_insert($node) {
  $top = views_get_view_result('top_3');

  $found = false;  
  foreach($top as $key => $value) {
    if ($value->nid == $node->nid) {
        $found = true;
        break;
    }
  }

  if ($found) {
    $ranking = $key + 1;
    $node->field_hoogste_ranking[LANGUAGE_NONE][0]['value'] = $ranking;
    field_attach_update('node', $node);
  }
}

Alternatively, you can try and use hook_node_presave.

share|improve this answer
    
$value->nid == $node->nid should be wrong as node has yet not been written into the DB and the View couldn't have got the nid using node_load. –  AgA Feb 2 at 13:38
    
@AgA: It does work, because hook_node_insert is activated after the node has been saved to the database. –  Jeroen Feb 2 at 14:17
    
There's only a ; missing at the end of the last code line. Perhaps you can change it for future users. –  Jeroen Feb 2 at 14:18
    
@Jeroen, thanks. I added the missing semicolon. –  Evil E Feb 2 at 17:00
    
No problem. I'm the one who should thank you. It's works perfectly! –  Jeroen Feb 2 at 17:01
add comment

You have to use hook_node_presave()

if you use hook_node_insert() then the value will not be saved in $node object

share|improve this answer
    
hook_node_presave() is invoked before node is actually created while hook_node_insert() is invoked after nodes get created, so you will find nid in case of hook_node_insert() but in case of hook_node_presave(). –  arpitr Feb 1 at 22:36
1  
Yes.. you are right .. I wrote that by mistake .. Yesterday I faced the problem if hook_node_insert.. which is not saving field value associated with $node object... while node_presave solved my problem.. Note: I have updated my answer.. thanks –  Tareq Assaf Feb 1 at 22:43
add comment

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.