0

I am attempting to programmatically insert some field data when a new node is being created using the following code:

function mymodule_node_insert($node) {

    $dialects = array('en-AU', 'en-NZ', 'en-US');

    node_object_prepare($node);
    foreach ($dialects as $dialect) {
        $node->translations->data[$dialect]['entity_id'] = $node->nid;
        $node->translations->data[$dialect]['entity_type'] = 'node';
        $node->translations->data[$dialect]['revision_id'] = 3;
        $node->translations->data[$dialect]['language'] = $dialect;
        $node->translations->data[$dialect]['source'] = 'en';
        $node->translations->data[$dialect]['uid'] = 1;
        $node->translations->data[$dialect]['status'] = 1;
        $node->translations->data[$dialect]['translate'] = 0;
        $node->translations->data[$dialect]['created'] = time();
        $node->translations->data[$dialect]['changed'] = time();

        $node->field_page_body[$dialect] = $node->field_page_body['en'];
    }
    node_save($node);       
}

However, I recieve the following error:

enter image description here

I understand it's complaining about the same node ID being saved twice but I'm not sure how to avoid this exception and update my new node with the programmatically set data?

It's important I execute my code at hook_node_insert because there are hundreds of nodes to be imported later and they all need the same treatment.

The same code works when tested in mymodule_init().

0

You cannot call node_save($node) during hook_node_insert($node).

According to the documentation, that hook is invoked during the middle of the database transaction, so the node isn't committed to the database yet. So when you invoke node_save, the old node you're attempting to update doesn't exist yet, and it will attempt to create a new node (with the same ID). Thus, the collision.

  • Thanks for your response and I apologise if my question was not clear but I had already deduced that issue. My question is realy more about how I can get around this issue. I still need to insert data into the node as it is being created. I'm sure my code is implemented in the right hook but the code itself is "faulty" - what do I do to fix that? – sisko Oct 10 '17 at 9:44
  • Oh. In that case, won't hook_node_presave work for your purposes? Do you absolutely need the nid to exist for the data you're adding? – vomitHatSteve Oct 10 '17 at 20:09
  • I did try hook_node_presave but the result was essentially a crash – sisko Oct 10 '17 at 23:51
  • Could you clarify? What error did you receive? Or does it not really matter per your answer below? – vomitHatSteve Oct 11 '17 at 17:19
  • I appreciate your dilligence and persistence but the problem is solved. The error was a white screen with raw text. It was one of those that when beyond php and apache issues and shows something has absolutely gone wrong with the system. – sisko Oct 11 '17 at 20:48
0

I found the work around to this issue here.

Use field_attach_update('node', $node);

instead of:

node_save($node);

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.