1

I am calling node_load() function in a custom module and it returns only part of the variables like title, uid, vid, status (all the meta), but it never returns actual field contents. I tried node_page_view(), tried menu_get_object() - all in vain, no field values.

What am I doing wrong? Is this because maybe my module is calling node_load() too early before the saving node, but then how it has already $node->title, $node->nid and other values? If they are some kind of prepared values, then how can I get the same prepared value for a custom field?

And here is the code from the Community documentation page Sending Notification Messages that I am using in my module:

/*
 * Implements hook_node_insert().
 */
function foo_node_insert($node) {

  $message = message_create('foo_message_type', array('uid' => $node->uid));

  $wrapper = entity_metadata_wrapper('message', $message);
  $wrapper->field_node_ref->set($node);

  $options = array(
     'rendered fields' => array(
       'message_notify_email_subject' => 'field_rendered_subject',
       'message_notify_email_body' => 'field_rendered_body',
     ),
  );

  message_notify_send_message($message, $options);
}

and it works just fine for the message subject, however for some reason I can't get node body to be mailed properly.

I tried to see the content of the $node variable in the following code from example module:

elseif (isset($message['params']['message_entity']->field_node_ref)) {
    // This is a node.
    $nid = $message['params']['message_entity']->field_node_ref[LANGUAGE_NONE][0]['target_id'];
    $node = node_load($nid);
    $messageid_params['uid'] = $node->uid;
    $messageid_params['nid'] = $node->nid;
    $messageid_params['cid'] = 0;
    $messageid_params['time'] = $node->created;
    if (variable_get('mailcomment_alter_subjects', 1)) {
      $subject = $message['subject'];
      $subject = variable_get('site_name', '') ? '[' . variable_get('site_name', '') . '] ' . $node->title : $subject;
      $message['subject'] = $subject;
    }
    dpm($node);
   }

and unfortunately my custom node field always returns empty:

field_description (Array, 0 elements)
$...->field_description
  • hook_node_insertis invoked from node_save() after the database query that will insert the node into the node table is scheduled for execution, after the type-specific hook_insert() is invoked, and after field_attach_insert() is called. Therefore, the node is already created. Is there a way that you can show your code? – Gulok Jul 30 '16 at 3:14
  • You should show the code you are using. node_load() loads the values from the database, but it stores the value in a temporary cache too. I have never seen the behaviour you are seeing, thought. – kiamlaluno Jul 30 '16 at 3:58
1

All that I needed to do was using

$node = node_load($nid, null, true);

instead of just

$node = node_load($nid);

and now the node is loading as expected.

|improve this answer|||||
0

Try

hook_entity_postinsert();

from the Hook Post Action module. It loads field values always by Entity metadata wrappers.

|improve this answer|||||
  • Thanks for the idea, but I don't see how can I use that function in my example. My module does not create a node, it just retrieves node information from $message variable. Please see drupal.org/node/2180227 – Nick Jul 30 '16 at 12:02

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.