Would it be possible to use the Rules module to populate a cck field with the user's first name and last initial when registering on my canvas app?

I am hoping it is possible with the "when user registers" trigger and "populate field" action, but I am not sure what PHP code to use:

return array(
0 => array('value' => ' ')
);

Comments

Dave Cohen’s picture

Search this issue queue for other threads about rules module. There may be useful info.

I don't know if that trigger fires when fb_user.module creates a user. But it will call hook_fb_user(), so you can write some custom code there.

okmi’s picture

Here is my relatively bootleg way of doing it with profile fields. Hopefully you can try something similar in your own module.

function my_module_fb_user($op, $data, &$return) {
   $fb_app = isset($data['fb_app']) ? $data['fb_app'] : NULL;
   $fb = isset($data['fb']) ? $data['fb'] : NULL;

   $fbu = fb_facebook_user();
   $info = $GLOBALS['_fb']->api($fbu);

   $username = $info['name'];
   $names = explode(" ", $username);
 
   $return['profile_first_name'] = $names[0];
   $return['profile_last_name'] = $names[1];

   return $return;
}
jghyde’s picture

here's my technique:

function MYMODULE_user($op, &$edit, &$account, $category = NULL) {
  switch ($op) {
    case 'insert':

  $fbu = fb_facebook_user();
  $fb_account = fb_api($fbu, array('access_token' => fb_get_token()));
  // Declare $userData as an array for later putting into the $user object
  $userData = array();
  // Declare $nodeData for content_profile node type 'profile' creation, or updating, as required.
  $nodeData = array();
  // Solve the email issue
  if (empty($account->mail)) {
    // The user's email in the $user object is blank. We need to add it:
    $userData['mail'] = $fb_account['email'];
  }
  if ($account->mail != $fb_account['email']) {
    // This one is dicey because we don't know if the user intentionally has an fb email
    // they want separate from this site's email address.
    // @TODO set the wizard up to request for clarification.
    // For salive, we'll make the fb email the real email.
    $res = db_result(db_query("SELECT COUNT(*) from {users} where mail='%s'", $fb_account->email));
    if ($res > 0) {
      // The user already has another account on the site with this email address,
      // So, let's just leave it alone, except to store it in a secret field on the user's content profile.
      $nodeData['field_fb_email_address'] = $fb_account['email'];
    }
    else {
      // Auto update the user's account email address using their Facebook email address
      // @TODO: Launch the user registration wizard to work out the differences?
      $userData['mail'] = $fb_account['email'];
    }
  }
  // Grab other data from Facebook for populating the user's 'profile' node:
  if (!empty($fb_account['first_name'])) {
    $nodeData['fname'] = $fb_account['first_name'];
  }
  if (!empty($fb_account['last_name'])) {
    $nodeData['lname'] = $fb_account['last_name'];
  }
// ... and etc ...

// saving to user_save:
  if (count($userData) > 0) {
    if (!user_save($account, array('mail' => $userData['mail']))) {
      watchdog('Facebook', 'The user FB information failed to save into the user object. Data: %email', $userData['mail'], WATCHDOG_ERROR);

// ... and then, since I am using content profile, save the $nodeData into a profile node type...
    }
  }
  break;
  }
}