1

I am trying to execute a Rule based on the condition of whether the currently logged in user, is currently viewing their node (a node they are the author of), or viewing someone else's node. The Rule event/trigger I am using is "User saves a Status" from Statuses module. Next I have used the Rules condition "Execute Custom PHP code", and set an if statement that checks whether the current node uid (the uid of the node's author) equals the current user uid:

if ($node->uid == $user->uid) {
  return TRUE;
}
else {
  return FALSE;
}

This is causing a bunch of "Trying to get property of non-object" errors, as both the $node and $user variables are not being recognised. I'm not sure why this is the case, do I need to use the node_load() and user_load() functions before using these variables? If so how?

3 Answers 3

2

I have managed to work it out! Below is the code for checking whether the current user is viewing content they are an author of:

global $user;
if (arg(0) == 'node' && is_numeric(arg(1))) {
  // Get the nid
  $nid = arg(1);

  // Load the node if you need to
  $node = node_load($nid);
}

if($node->uid == $user->uid){
return TRUE;
} else {
return FALSE;
}

Hope this is helpful to someone.

1
  • This was indeed useful to me Commented Apr 7, 2015 at 6:18
1

You don't need to write any PHP code to achive that.

  1. Create a new rule
  2. React on event: Content is viewed (under Node section)
  3. Add condition: Data comparison (under Data section). Continue.
  4. Data to compare, data selector: node:author. Continue.
  5. Data value, data selector: site:current-user. Save.

UPDATE:

According to you latest edit, my above answer becomes invalid. As I already written in comments, I am not sure if $node object is loaded in your script, however I am almost sure that the $user object is not. You can load the currently logged in user adding:

global $user;

at the beginning of you code block. Since I never used PHP in UI, I am not sure if there are tokens available to use. Look around the UI to find out this information, and if they are available, try to make use of them in your code.

9
  • Yes I do know about the data comparison condition but my Rule event/trigger is actually "User saves a Status" from the Statuses module (drupal.org/project/statuses), not "Content is viewed". This is why I tried using PHP since the node:author token isn't available when using the above Rule event/trigger. Any advice how I could do this?
    – FrontEnd
    Commented Apr 5, 2014 at 8:59
  • Please add this info to your question.
    – Elin Y.
    Commented Apr 5, 2014 at 9:03
  • Did you enabled 'Statues Rules' module?
    – Elin Y.
    Commented Apr 5, 2014 at 9:09
  • I have updated my question, and yes Statuses Rules is enabled. If there is a way to do this without the custom PHP I would prefer it, but I haven't been able to find one yet which is why I'm trying to do things this way.
    – FrontEnd
    Commented Apr 5, 2014 at 9:17
  • I didn't use PHP code execution in Rules before, since I am not a friend of using PHP code in UI (which has security implications). I cannot help you much about that, since I am not sure if the $node object is already loaded. However I am almost sure that the $user object is not. Please add global $user; just before your code and try again.
    – Elin Y.
    Commented Apr 5, 2014 at 9:24
0

I have a little bit of a different method. It's very similar to FrontEnd's method, but I try to stay away from arg() as much as possible (for better or worse). It uses menu_get_object() so this won't work in D8, but I've not moved into D8 yet for anything I'm doing.

Here she is:

global $user;
// Logged in user
$uid = $user->uid;

$node = menu_get_object();
if (!empty($node)) {
  // Node creator ID
  $nuid = $node->uid;
}

if ($uid==$nuid) {
  $creator = TRUE;
} else {
  $creator = FALSE;
}

Then, if I need to display specific information to the content creator I just use:

if ($creator) {
  // Hello Creator
}

Now I've got uid of the logged in user and the content creator to use further down the function as well.

Your Answer

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

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