0

I have the code below, and I want to update the fields on my node using hook_nodeapi(). How do I set the fields on insert?

/*
 * Implements hook_form_alter().
 */

function feature_contact_form_form_alter(&$form, $form_state, $form_id) { 
  if ($form_id == 'contact_form_node_form') {
    $form['field_user_agent']['#type'] = 'hidden';
    $form['field_ip_address']['#type'] = 'hidden';
    $form['buttons']['submit']['#weight'] = 1;
  }
}

/*
 * Implements hook_nodeapi().   
 */
function feature_contact_form_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  if ($node->type == 'contact_form') {
    if ($op == 'insert') {
      function getRealIpAddr() {
        if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
          $ip = $_SERVER['HTTP_CLIENT_IP'];
        }
        elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
          $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        }
        else {
          $ip = $_SERVER['REMOTE_ADDR'];
        }
        return $ip;
      }

      $ipaddress = getRealIpAddr($ip);
      $node->field_user_agent = array($ipaddress);
      $node->field_ip_address = array($_SERVER['HTTP_USER_AGENT']);
    }
  }
}
4
  • By fields do you mean CCK fields or (to use D7 speak) 'properties' of the node (e.g. uid, type, etc.)?
    – Clive
    Dec 4, 2012 at 17:42
  • fields in my form are also properties in my node object depending on the context, no? Dec 4, 2012 at 17:43
  • Sure, but any properties the node schema doesn't know about won't be persisted automatically. For example if you have $form['test'] in your form, you'll be able to access it at $node->test in the insert op, but Drupal doesn't know to do anything with that data so it won't write it to the db or load it back up when the node is reloaded. I might've misunderstood what you mean though, sorry if that's the case
    – Clive
    Dec 4, 2012 at 17:46
  • that is correct. you understood well. How do I persist the data across and insert it properly? Is hook_nodeapi what I need for this? thanks. Dec 4, 2012 at 17:49

1 Answer 1

2

As mentioned in the comments any extra fields you add to to a node form won't automatically be persisted to the database, the node module only takes care of the fields that it knows about.

Traditionally there are 2 options:

  1. Use CCK instead and add fields to the node that way. Then you don't need to take care of data storage/loading yourself.
  2. Add your own table for data storage using hook_schema(), making sure you have a column for nid (node id). Then in hook_nodeapi() use the insert and update ops to run a db_query() to insert/update the data in that table for the current node.

    Then you just need to load that data back from the DB on to the $node object in the load op of hook_nodeapi(). Your data will then be available at $node->field_name when you have a node object loaded with node_load().

5
  • I already have added the fields using CCK. Fields: field_user_agent, field_ip_address already exist. I just set them as hidden on hook_form_alter. Dec 5, 2012 at 9:49
  • I wasn't sure so I included it anyway, just ignore that bit of it's not relevant :)
    – Clive
    Dec 5, 2012 at 9:51
  • I need to use the code in hook_nodeapi to capture the user_agent info and IP address but I still don't understand how to save that. Is the only option to use hook schemea and do the inserts manually? Dec 5, 2012 at 9:52
  • I think we might be misunderstanding each other here...so field_user_agent and field_ip_address are already CCK fields? I was under the impression they weren't. If you want to manually set the value you can do so in the presave op of hook_nodeapi() (before the node data is actually saved) and set a value like $node->field_user_agent[0]['value'] = $ua;. Does that make more sense?
    – Clive
    Dec 5, 2012 at 10:06
  • Yeah I was having a brain freeze there. I was just about to post $node->field_user_agent['0']['value'] = $_SERVER['HTTP_USER_AGENT']; $node->field_ip_address['0']['value'] = $ipaddress; Dec 5, 2012 at 10:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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