Last updated June 7, 2014. Created on July 28, 2013.
Edited by acabouet, Alpinist1974. Log in to edit this page.

I recently encountered a situation where I wanted to redirect a user to create a custom content type on login if that content doesn't already exist for the user. Profile2 and Account both provide token support for these fields through Rules, so depending on your needs and how your content is related to the user, you might be able to use these. For my custom type, I was unable to use tokens, so I had write some custom PHP to test if content exists of a certain content type for a given user. Here's what worked for me:

1. Create a rule (you must download and install the contrib Rules module to do this). I called mine "Redirect User at Login."
2. Select React on Event > User > User has logged in.
3. From Conditions, select the condition to add: PHP > Execute custom PHP code.
4. On the Add a new condition page, in the PHP code, Value field, enter (without PHP brackets):

global $user;
$result = db_query("SELECT COUNT(nid) FROM node JOIN users ON node.uid = users.uid WHERE node.status = :status AND users.uid = :uid AND node.type = :type", array(":status"=> 1, ":uid" => $user->uid, ":type" => 'configuration_1'))->fetchField();
if ($result >= 1){
return FALSE;
}
else{
return TRUE;
}

In this code, the SQL query checks the database for a user who has created one or more nodes of the type configuration_1. If one or more nodes exist for this user, the condition evaluates to false, and the user will not be redirected to the node/add page. If no nodes exist for the user, the code returns true, and the user will be redirected. Node status "1" means the node is published. Be sure to declare the global $user variable before the query.

5. When you have saved the code, go to Actions, Add action. Select System > Page Redirect. Now add the URL of the page you want to redirect the user to. In my case, it's node/add/configuration-1.

Save your rule and set the weight so that it is triggered in the proper order in relation to any other rules you may have. Make sure the rule is set to Active. Test the rule. You're good to go.

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

pal4life’s picture

Hi,
Let me say I reached this post since my question was around whether using Php inside the rules module is a safe option or not? So by default I am not contradicting what you did. I am just skeptical how safe this is or how much of a good coding practice this is.

Let me rephrase my question. I came here via this question - http://drupal.stackexchange.com/questions/108443/is-it-safe-to-use-php-w... . Both the responders there feel different about this situation. Do you feel putting the php in action taken in this situation would have been a better coding practice, by adding it in a custom module and implementing it via hook_rules_condition_info()?

mooru’s picture

Can this be done using other rules events like when cron runs to check if the user has content of node type X and node should be posted on a certain day? I tried this

<?php
global $user;
$result = db_query("SELECT COUNT(nid) FROM node JOIN users ON node.uid = users.uid WHERE node.status = :status AND node.created > :date AND users.uid = :uid AND node.type = :type", array(":status"=> 1, ":date:" => strtotime('30 August 2014'), ":uid" => $user->uid, ":type" => 'configuration_1'))->fetchField();
if (
$result >= 1){
return
FALSE;
}
else{
return
TRUE;
}
?>
mooru’s picture

After battling with this, i finally found a way to use the flag module to get it done...