This is a rule for the rule module I just wrote. It displays a message and redirects the user to the registration form after he clicked on Facebook connect, but has no account on the site yet. My users did not understand, that after clicking on connect, they still had to go through the normal registration proces...

I am using the OpenID selector module, this may have changed this behaviour.

Here is the import/export code for my rule:

array (
  'rules' => 
  array (
    'rules_redirect_facebook_connect' => 
    array (
      '#type' => 'rule',
      '#set' => 'event_init',
      '#label' => 'Redirect to registration from when user connects to facebook',
      '#active' => 1,
      '#weight' => '0',
      '#categories' => 
      array (
      ),
      '#status' => 'custom',
      '#conditions' => 
      array (
        0 => 
        array (
          '#type' => 'condition',
          '#settings' => 
          array (
            'roles' => 
            array (
              0 => 1,
            ),
            'operation' => 'OR',
            '#argument map' => 
            array (
              'user' => 'user',
            ),
          ),
          '#name' => 'rules_condition_user_hasrole',
          '#info' => 
          array (
            'label' => 'User is anonymous',
            'label callback' => false,
            'arguments' => 
            array (
              'user' => 
              array (
                'type' => 'user',
                'label' => 'User',
              ),
            ),
            'module' => 'User',
          ),
          '#weight' => -1,
        ),
        1 => 
        array (
          '#info' => 
          array (
            'label' => 'User is connected to Facebook (PHP)',
            'label callback' => false,
            'module' => 'PHP',
            'eval input' => 
            array (
              0 => 'code',
            ),
          ),
          '#name' => 'rules_condition_custom_php',
          '#settings' => 
          array (
            'code' => '// 7 is the id of the role set in the FB module
// $GLOBALS[\'user\'] and $user are not equivalent: the former is modified by the fb_user module, the latter is not
return isset($GLOBALS[\'user\']->roles[7]);',
            'vars' => 
            array (
              0 => 'user',
            ),
          ),
          '#type' => 'condition',
          '#weight' => 0,
        ),
        2 => 
        array (
          '#weight' => 0,
          '#type' => 'condition',
          '#settings' => 
          array (
            'code' => 'return FALSE === strpos ( request_uri(), \'user/register\' );',
            'vars' => 
            array (
            ),
          ),
          '#name' => 'rules_condition_custom_php',
          '#info' => 
          array (
            'label' => 'Path is not /user/register',
            'label callback' => false,
            'module' => 'PHP',
            'eval input' => 
            array (
              0 => 'code',
            ),
          ),
        ),
        4 => 
        array (
          '#type' => 'condition',
          '#settings' => 
          array (
            'code' => '// Otherwise, the event would be triggered again, when the Captcha of the registration form is loaded.
return FALSE === strrpos ( request_uri(), \'/image_captcha/\' );
',
            'vars' => 
            array (
            ),
          ),
          '#name' => 'rules_condition_custom_php',
          '#info' => 
          array (
            'label' => 'Path is not /image_captcha/*',
            'label callback' => false,
            'module' => 'PHP',
            'eval input' => 
            array (
              0 => 'code',
            ),
          ),
          '#weight' => 0,
        ),
        3 => 
        array (
          '#weight' => 1,
          '#info' => 
          array (
            'label' => 'User has just clicked on FB connect (PHP)',
            'label callback' => false,
            'module' => 'PHP',
            'eval input' => 
            array (
              0 => 'code',
            ),
          ),
          '#name' => 'rules_condition_custom_php',
          '#settings' => 
          array (
            'code' => '$urlnew  = FALSE !== strpos ( request_uri(), \'?_fb_js_fbu=\' );
$urlnew &= FALSE === strpos ( request_uri(), \'?_fb_js_fbu=0\' );
return $urlnew;',
            'vars' => 
            array (
            ),
          ),
          '#type' => 'condition',
        ),
      ),
      '#actions' => 
      array (
        0 => 
        array (
          '#type' => 'action',
          '#settings' => 
          array (
            'message' => 'Your session has now been linked to your Facebook account, but you still need to register on this website. However, from then on, you just need to click on the Facebook icon to log in.',
            'error' => 0,
          ),
          '#name' => 'rules_action_drupal_message',
          '#info' => 
          array (
            'label' => 'Show a configurable message on the site',
            'module' => 'System',
            'eval input' => 
            array (
              0 => 'message',
            ),
          ),
          '#weight' => 0,
        ),
        1 => 
        array (
          '#weight' => 0,
          '#info' => 
          array (
            'label' => 'Redirect to registration form',
            'label callback' => false,
            'module' => 'System',
            'eval input' => 
            array (
              0 => 'path',
              1 => 'query',
              2 => 'fragment',
            ),
          ),
          '#name' => 'rules_action_drupal_goto',
          '#settings' => 
          array (
            'path' => 'user/register',
            'query' => '<?php $url = parse_url(request_uri()); $url =  $url[\'query\'] . \'&destination=\' . $url[\'path\']; echo $url; ?>',
            'fragment' => '',
            'force' => 1,
            'immediate' => 0,
            '#eval input' => 
            array (
              'rules_input_evaluator_php' => 
              array (
                'query' => 
                array (
                ),
              ),
            ),
          ),
          '#type' => 'action',
        ),
      ),
      '#version' => 6003,
    ),
  ),
)

Comments welcome :)

Cheers,
Ingo

Comments

ingomueller.net’s picture

Hold on, that does not work yet...

ingomueller.net’s picture

Status: Active » Closed (won't fix)

I give up. I don't understand rules. Code in my rule evaluation var_dumps the return value of my condition as FALSE, but the rules debug log evaluates the condition as true. I can't fight that. Nevermind, sorry for disturbing...

ingomueller.net’s picture

Status: Closed (won't fix) » Active

I did not give up... :P

Now, it works as I intended... Here's the rule:

array (
  'rules' => 
  array (
    'rules_redirect_facebook_connect' => 
    array (
      '#type' => 'rule',
      '#set' => 'event_init',
      '#label' => 'Redirect to registration from when user connects to facebook',
      '#active' => 1,
      '#weight' => '0',
      '#categories' => 
      array (
      ),
      '#status' => 'custom',
      '#conditions' => 
      array (
        0 => 
        array (
          '#weight' => -1,
          '#info' => 
          array (
            'label' => 'User is anonymous',
            'label callback' => false,
            'arguments' => 
            array (
              'user' => 
              array (
                'type' => 'user',
                'label' => 'User',
              ),
            ),
            'module' => 'User',
          ),
          '#name' => 'rules_condition_user_hasrole',
          '#settings' => 
          array (
            'roles' => 
            array (
              0 => 1,
            ),
            'operation' => 'OR',
            '#argument map' => 
            array (
              'user' => 'user',
            ),
          ),
          '#type' => 'condition',
        ),
        1 => 
        array (
          '#weight' => 0,
          '#type' => 'condition',
          '#settings' => 
          array (
            'code' => '// 7 is the id of the role set in the FB module
// $GLOBALS[\'user\'] and $user are not equivalent: the former is modified by the fb_user module, the latter is not
return isset($GLOBALS[\'user\']->roles[7]);',
            'vars' => 
            array (
              0 => 'user',
            ),
          ),
          '#name' => 'rules_condition_custom_php',
          '#info' => 
          array (
            'label' => 'User is connected to Facebook (PHP)',
            'label callback' => false,
            'module' => 'PHP',
            'eval input' => 
            array (
              0 => 'code',
            ),
          ),
        ),
        2 => 
        array (
          '#info' => 
          array (
            'label' => 'Path is not /user/register',
            'label callback' => false,
            'module' => 'PHP',
            'eval input' => 
            array (
              0 => 'code',
            ),
          ),
          '#name' => 'rules_condition_custom_php',
          '#settings' => 
          array (
            'code' => 'return FALSE === strpos ( request_uri(), \'user/register\' );',
            'vars' => 
            array (
            ),
          ),
          '#type' => 'condition',
          '#weight' => 0,
        ),
        4 => 
        array (
          '#weight' => 0,
          '#info' => 
          array (
            'label' => 'Path is not /image_captcha/*',
            'label callback' => false,
            'module' => 'PHP',
            'eval input' => 
            array (
              0 => 'code',
            ),
          ),
          '#name' => 'rules_condition_custom_php',
          '#settings' => 
          array (
            'code' => '// Otherwise, the event would be triggered again, when the Captcha of the registration form is loaded.
return FALSE === strrpos ( request_uri(), \'/image_captcha/\' );
',
            'vars' => 
            array (
            ),
          ),
          '#type' => 'condition',
        ),
        3 => 
        array (
          '#type' => 'condition',
          '#settings' => 
          array (
            'code' => '$urlnew = FALSE !== strpos ( request_uri(), \'?_fb_js_fbu=\' );
$urlnew &= FALSE === strpos ( request_uri(), \'?_fb_js_fbu=0\' );

if( $urlnew ) return true;
else return false;',
            'vars' => 
            array (
            ),
          ),
          '#name' => 'rules_condition_custom_php',
          '#info' => 
          array (
            'label' => 'User has just clicked on FB connect (PHP)',
            'label callback' => false,
            'module' => 'PHP',
            'eval input' => 
            array (
              0 => 'code',
            ),
          ),
          '#weight' => 1,
        ),
      ),
      '#actions' => 
      array (
        0 => 
        array (
          '#weight' => 0,
          '#info' => 
          array (
            'label' => 'Show a configurable message on the site',
            'module' => 'System',
            'eval input' => 
            array (
              0 => 'message',
            ),
          ),
          '#name' => 'rules_action_drupal_message',
          '#settings' => 
          array (
            'message' => 'Your session has now been linked to your Facebook account, but you still need to register on this website. However, from then on, you just need to click on the Facebook icon to log in.',
            'error' => 0,
          ),
          '#type' => 'action',
        ),
        1 => 
        array (
          '#weight' => 0,
          '#info' => 
          array (
            'label' => 'Page redirect',
            'module' => 'System',
            'eval input' => 
            array (
              0 => 'path',
              1 => 'query',
              2 => 'fragment',
            ),
          ),
          '#name' => 'rules_action_drupal_goto',
          '#settings' => 
          array (
            'path' => 'user/register',
            'query' => '<?php $url = parse_url(request_uri()); $url = $url[\'query\'] . \'&destination=\' . $url[\'path\']; echo $url; ?>',
            'fragment' => '',
            'force' => 1,
            'immediate' => 1,
            '#eval input' => 
            array (
              'rules_input_evaluator_php' => 
              array (
                'query' => 
                array (
                ),
              ),
            ),
          ),
          '#type' => 'action',
        ),
      ),
      '#version' => 6003,
    ),
  ),
)

Just out of curiousity: can someone explain why the two following pieces of code do not have the same result, EVEN :


$urlnew = FALSE !== strpos ( request_uri(), \'?_fb_js_fbu=\' );
$urlnew &= FALSE === strpos ( request_uri(), \'?_fb_js_fbu=0\' );

if( $urlnew ) return true;
else return false;


$urlnew = FALSE !== strpos ( request_uri(), \'?_fb_js_fbu=\' );
$urlnew &= FALSE === strpos ( request_uri(), \'?_fb_js_fbu=0\' );

return $urlnew;

Cheers,
Ingo

Dave Cohen’s picture

I don't understand rules, but it is pretty easy to configure Drupal for Facebook to create that account automatically, if that is what you want. (fb_user.module will do it for you) You should send users to registration page if your site requires some extra info that you can't learn from facebook.

ingomueller.net’s picture

Ok, good point! At the moment, I do need extra information, so there still a use case. But maybe I can use rules and FB connect to retrieve them automatically from Facebook...

escore’s picture

Checkout this thread for ideas: http://drupal.org/node/878282. I get user data from FB on account creation without using Rules. When DFF is about to save the new user, it exposes a hook for you to pick up. E.g., I use it to get the user language (se the code at http://drupal.org/node/878282#comment-4161396). If you want to pre-populate the registration form with data from FB, you could use the form_alter hook. My (limited) experience with Rules is that is a great glue for making different modules interact, or if you want to create complex or reconfigurable actions. If it's a central part of the design in my module that doesn't need to be reconfigurable, I'd rather use a hook.