1

Within an instance of hook_submit (for a custom form), I'm trying to iterate over a list of nodes, updating each of them. The general outline is

$x = node_load_multiple($nids);
$data = array(--some data--);

foreach ($data as $row) {
  $node = $x[$row['nid']];
  $node->field_some_field['und'][0]['value'] = $row['some_field'];
  node_save($node);
}

drupal_set_message('Nodes updated');

The problem I'm having is that rather than looping through the whole data array and then posting my confirmation message on the form page, Drupal updates the first node, exits the loop, and redirects to the user's home page with the message 'contenttype TITLE updated.'

Possibly relevant: I could have sworn this code block worked when the site was 7.2, but after updating to 7.23 I'm seeing this.

  • 4
    You've got something, somewhere, performing a redirect when nodes are created/updated. Maybe a rule? A custom module implementing hook_node_insert() or hook_node_update()? – Clive Oct 8 '13 at 22:19
  • Yep, that was it. Something I'd forgotten about in a hook_node_update... Thanks! – bricoleur Oct 9 '13 at 0:12
  • bricoleur If you could post what you found as an answer, it might be useful for someone in the future. @GisleHannemyr "too localized" was removed from close reasons for a reason, wasn't it? – Mołot Oct 9 '13 at 7:39
2

To summarize one of the comments, when you encounter weirdness with the API, you need to do two things.

First, look at the API call you are using, and see what hooks it invokes. Then, check your code for any hook implementations that could cause the weirdness. In this case node_save() invokes a few: hook_node_presave(), hook_entity_presave, hook_node_update, and hook_node_update for nodes that exist.

Second, you need to check any Rules that are attached to the the API call you are doing. In this case, you would check for rules that run when nodes are saved.

Doing these two things will usually lead you to the problem.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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