0

I am using "Execute custom PHP" as a condition in a Rule which is being triggered when a new Status is saved. Here is the PHP I am using, it keeps causing "Trying to get property of non-object" errors whenever I create a new status:

if($sender->uid == $recipient-id){
return TRUE;
} else {
return FALSE;
}

Basically what I am trying to do is detect cases when the sender is sending a status message to himself so I can use an appropriate Rules action, and also the opposite:

if($sender->uid != $recipient-id){
return TRUE;
} else {
return FALSE;
}

I have tried a number of different variable combinations but I can't work out what I am doing wrong. Any suggestions?

  • it's basically saying that $sender is not an object yet you are trying access it in that way. What is sender and how are you passing it into the rule? – Daniel Harper Mar 22 '14 at 15:57
  • The trigger for the rule is "user saves a status" (from Statuses module), then the rules condition is set to "execute custom php" (from the builtin php filter), and finally a heartbeat activity is logged. So the php I posted above is sitting in the rules condition. Does that help? – FrontEnd Mar 22 '14 at 23:05
0

This should do what you want.

if($status->sender == $status->recipient) {
  drupal_set_message('A match');
}
else{
  drupal_set_message('Not a match');
}

Rules Code

{ "rules_status_test" : {
    "LABEL" : "Status Test",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "php", "rules", "fbss_rules" ],
    "ON" : { "statuses_save" : [] },
    "IF" : [
      { "php_eval" : { "code" : "if($status-\u003Esender == $status-\u003Erecipient) {\r\n  drupal_set_message(\u0027A match\u0027);\r\n}\r\nelse{\r\n  drupal_set_message(\u0027Not a match\u0027);\r\n}" } }
    ],
    "DO" : []
  }
}
|improve this answer|||||
  • Thanks for the code but I can't get this to work the way it is unfortunately. When I modify your code to look for the sender id and recipient id it then does work as intended, but I still get a "Trying to get property of non-object" error each time. I don't know if it is a token issue maybe, statuses module is notorious for this. – FrontEnd Apr 5 '14 at 5:15
  • why do you need to look for the id? When comparing $status->sender and $status->recipient you are comparing two objects of the same type. It works on my installation. My answer doesn't use tokens it uses the variable directly. I've added a the rule which works on my clean install. – Daniel Harper Apr 5 '14 at 16:26

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.