3

Drupal 6.25, CCK 2.9

I have a node of type EVENT. The EVENT node contains several CCK fields that allow users to submit events to our calendar which go in to a workflow. If the user requested IT support at their event, the IT support person is e-mailed. If they will be hosting an event with food, the catering staff is notified.

Everything works great except that we can edit the event later upon their request. If the edits that we make change one of these values that is used to notify staff, I need the system to automatically notify those people.

I know how to handle notification and everything else works well, but I don't know how to detect if the state of a single field is changing.

DETAILED EXAMPLE:

--- Event information... ---

Will you require IT support at your event?

  • I need my lecture recorded.
  • We will require someone on hand to assist with software installation.
  • Video conferencing
  • Special request

(multiple checkboxes)

--- More event information ... ---

If that EVENT node is submitted, approved, and later edited to change the value of that field so that one of the boxes is checked, I need to handle that.

I have a custom module to handle this in and have experience writing validation and submit handling, but I'm not sure what values of $form or $form_state are helpful to this task. I thought that I might store the value of the field as it was originally as a hidden field with a hook_form_FORM_ID_alter and then check that hidden value against the submitted value but it's hard to get CCK values in to hidden fields because you have to capture the value in an #after_build function and... I'm just getting stuck and thought I'd ask for help.

Thanks in advance for your time and consideration!

EDIT - My solution based on the suggestion from Jimajamma below:

function sitecustom_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  // If we are saving an EVENT node...
  if ('presave' == $op && 'event' == $node->type) {
    // If the node already exists...
    if ($existing = node_load($node->nid)) {
      // If the FIELD_EVENT_TECH_SUPPORT value has changed...
      if ($node->field_event_tech_support == $existing->field_event_tech_support) {
        // Log the change.
        watchdog('event_edit', 'FIELD_EVENT_TECH_SUPPORT value did not change');
      } else {
        // Notify people accordingly.
        // TODO: Notification of staff of changed tech support for EVENT.
        // Log the notification.
        watchdog('event_edit', 'Event edited with FIELD_EVENT_TECH_SUPPORT value changed. Staff notified.');
      }
    }
  }
  // Handle other nodeapi uses, validation, etc.
}

And I'm not exactly sure if it's correct to do if ($existing = node_load($node->nid)) because that block seems to execute even when an EVENT node is being created from scratch. I know that I could use if ($existing->created != $existing->changed) but I haven't thought through everything yet and exactly what I want to test, etc. For now, using what I've shown above won't trigger the notification in this case IF the node is new.

I guess I just wanted to get something up here in terms of a solution in case people find this in the future. I will move on to exploring how to make this easier to do/find with RULES and ACTIONS as time allows.

  • Have you considered using the rules module? You could write a rule that says if that field changes upon node update, then send an email to the IT users – Stan Ascher Mar 1 '12 at 3:35
  • I do use the rules module quite a bit with this, actually, but handling a field CHANGING isn't available. Maybe I need to look more in that direction in order to provide a better solution for people going forward. It's possible that there is a way to do it with rules and actions that I didn't see/discover and if there isn't, there should be and I could write a patch that would provide this more easily than using custom PHP rules. – mechler Mar 1 '12 at 15:42
3

I haven't done this, but my first idea would be to do this in nodeapi() and do something along the lines of (this horrid pseudo code):

if ( $op == 'presave' and $node->type == 'event' ) :
   $existing = node_load($node->nid);
   if ( $existing ) :
       check your fields in $node against the $existing node
       if they are different do something

and take into account you may no longer need IT or catering, so that needs to be conveyed as well as if you need it afterall.

  • This is an approach that I had considered, too, but I didn't think to load the node for comparison. This is a good idea. I'll give it a try. – mechler Feb 29 '12 at 22:52

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.