1

Drupal 6 Question:

I have two content types - a Property and a Task. The Task content type has a CCK Node Reference field that references the Property node it belongs to. When I modify a certain field in the Property node, I need to automatically update the "field_task_status" within it's referring Task node to "Complete".

I've been able to accomplish this with Rules 1 and VBO, but It seems like building a multitude of VBO views and triggered rules just to handle switching the Task node's status to "complete" or "pending" is a bit overkill.

Is there a more efficient way of doing this through a small custom module? I've heard that VBOs are heavy on the server, and I don't want to run into problems with the VBOs bogging down site.

I was initially thinking that I could write a query to get the nid of the referring Task node, and use that to load that Task and subsequently modify the value of its "field_task_status" ... I'm not a developer by trade, but I can put the code together if I have some idea of where to start.

Edit: Here's my attempt at coding a custom module, it's not getting me what I want...am I even close?

function mymodule_task_updater($property)
{
 $task_id =db_result( db_query( 
 "SELECT tn.nid FROM content_type_task_notification tn, 
 content_field_property_reference p, node n 
 WHERE  n.nid=p.field_property_reference_nid 
 AND tn.field_task_notice_type_value='Realtor Assignment' 
 AND  p.field_property_reference_nid='%d' ", $property->nid ));

 $task = @node_load( $task_id );

if($task && $property->field_realtor_reference[0]['value'] == NULL)
   {
    $task->field_task_notice_status[0]['value'] = 'Pending';
    node_save($task);
   }
}
2

If you are careful you can use hook_node for this. You can check when node type property is saved, and load alter and save the task.

Make sure you put guards in place otherwise it is easy to end up with recursion.

  • This is how I would do it. – googletorp Jul 19 '11 at 8:35
  • Do you have a quick example using the hooks you've mentioned? I'm kinda lost on this one. – user2014 Jul 19 '11 at 18:51
2

Try this. Add this inaddition to your MYMODULE_task_updater function.

/**
 *  Implements hook_nodeapi()
 */
function MYMODULE_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'insert':
    case 'update':
      //check for correct type
      if ($node->type == 'property') {
        //check if certain field is modified
        if ($node->field_certain_modified_field['value'] == 'something different' ) {
           MYMODULE_task_updater($node);
        }
      }
      break;
  }
}

EDIT:

For Drupal 7 hook_nodeapi() has been replace by hook_node_update() & hook_node_insert. You will have to add the same code in both MYMODULE_node_update & MYMODULE_node_insert function.

0

Ok so I've got it working the way I want...let me know if this is the best way to do this. Everything seems to be working correctly with the following code:

  module_load_include('inc', 'node', 'node.pages'); 
/**
 *  Implements hook_nodeapi()
 */

 function MYMODULE_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'insert':
    case 'update':
    //check for correct type
      if (  $node->type == 'property' ) {
        MYMODULE_task_complete($node);   

     }
      break; 
  } 
}
function MYMODULE_task_complete( $node)
{
    $result =db_result(db_query( 
   "SELECT tn.nid 
   FROM content_type_task_notification tn, content_field_property_reference p
   WHERE  tn.nid=p.nid AND tn.field_task_notice_type_value='Realtor Assignment'
   AND p.field_property_reference_nid='%d' ",  $node->nid));

   $task =  node_load( $result );

    if($task )
    { // We need this here or else the system will just create a blank node

         if($node->field_realtor_reference[0]['uid'] )
        {
        $task->field_task_notice_status[0]['value'] = 'Complete';
        node_save($task);
         }
       else 
           {      
       $task->field_task_notice_status[0]['value'] = 'Pending';
       node_save($task);
        }               
        }

   }
  • ya looks right. – iStryker Jul 20 '11 at 1:54

Your Answer

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