I´m trying to create a custom integration with a module that keeps track of visiting records from an external database.
It seems to work just fine when I pass a certain node through the trigger, the node is recorded in the activity but the user object seems to get ignored. I'm curious what I'm doing wrong here. Whatever I seem to try, the UID is 1 in the DB record (has it something to do with the fact that the activity records are being added during a cron job?)

To clarify things, here is my code, why is the $user argument not being passed when the trigger is called?

<?php
  function x_visitorhistory_x_visitorhistory($op,$user,$node) {
    if (function_exists('_trigger_get_hook_aids')) {
      $aids = _trigger_get_hook_aids('x_visitorhistory', $op);
    
      switch ($op) {
        case 'insert':
          $context = array(
            'hook' => 'x_visitorhistory',
            'op' => $op,
            'user' => $user,
            'node' => $node,
          );
          break;

      }
      if ($aids) {
        actions_do(array_keys($aids), $user, $context);
      }
    } 
  }


function x_visitorhistory_activity_info() {
  $info = new stdClass();
  $info->api = 2;
  $info->name = 'x_visitorhistory';
  $info->object_type = 'x_visitorhistory';
  $info->objects = array('user' => 'user'); // array keys are the labels
  $info->hooks = array('x_visitorhistory' => array('insert'));
  $info->realms = array('x_visitorhistory' => 'X Visitor History');
  return $info;
}

?>

Comments

Scott Reynolds’s picture

Status: Active » Needs review

I believe that the user object is being used for the tokens (so if you have the token [user] it is based on the $user object) but when the activity is recorded to the database it has uid of the person running cron. If this is correct it is because of this set of lines around 516 of activity.module

  $record = new stdClass();
  $record->uid = (!empty($context[$type]->uid)) ? $context[$type]->uid : $user->uid;
  $record->op = $context['op'];
  $record->type = $activity_object->name;
  $record->nid = $nid;
  $record->created = time(); // for PHP4 compat. for D7 use REQUEST_TIME

So what that means is that in the context, it expects an object 'x_vistorhistory' that has a uid on it.

My suggestion is to change the $info->object_type = 'user', so this code for your activity_info. As the 'type' of activity is 'user'.

function x_visitorhistory_activity_info() {
  $info = new stdClass();
  $info->api = 2;
  $info->name = 'x_visitorhistory';
  $info->object_type = 'user';
  $info->objects = array('user' => 'user'); // array keys are the labels
  $info->hooks = array('x_visitorhistory' => array('insert'));
  $info->realms = array('x_visitorhistory' => 'X Visitor History');
  return $info;
}
eelke’s picture

Status: Needs review » Fixed

Thanks so much for your quick response, your suggestion solved it!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.