Error message

Deprecated function: implode(): Passing glue string after array is deprecated. Swap the parameters in drupal_get_feeds() (line 394 of /home1/tylerfra/public_html/includes/common.inc).

Drupal Convert User Relationships Module to User Reference Field

Category: 

So I was using the User Relationships module for a while on a project. It worked OK, but I got very tired of not having the flexibility of a user reference field. Since I was already using the Content Profile module for users, I just added a new user reference field to the Profile content type. The user reference field was configured for multiple values.

I then wrote a simple hook_menu item and a call back function that would actually handle the conversion from the User Relationships module to a User Reference field on my Content Profile setup.

In the following example I was using a relationship called "Favorites" and it had an id of 3 (according to the user relationship module's table in the mysql database). I also was using a user reference field on my Profile (content profile) content type with a machine name of field_dg_profile_favorites (the field is configured for unlimited values). If you use this code be sure to replace the integer id for the relationship mysql lookup, as well as use your field's machine name.

Note, since I had less than 200 relationships to convert I didn't use any kind of batch processing, I just went for it all it once. Be sure to be careful if you have tons and tons of relationships to convert.

Also, for some reason I had to run this code many times, because for user's that had multiple relationships, this code would only pop one relationship onto the new user reference field at a time. Probably something to do with the user_load, node_load, node_save combo of calls. Once I refreshed a bunch of times and saw output messages only from "A already in B's favorites", everything had worked just fine. So yeah, I'm gonna have to ask you to go ahead and come in on Saturday and fix that for me, MMMK?

This code goes into your hook_menu inside your custom module:

$items['user/conert-user-relationships-to-user-references'] = array(
  'title' => 'Covert User Relationship to User References',
  'page callback' => 'dg_user_relationships_convert_to_user_reference_field',
  'access arguments' => array('administer users'),
  'type' => MENU_CALLBACK,
  'file' => 'my_module.pages.inc'
);

If you have a file called my_module.pages.inc inside your module, then this callback function can go in there:

function dg_user_relationships_convert_to_user_reference_field () {

  $html = "Conversion time...<hr />";

  // grab a list of user relationships...
  $sql = " SELECT * FROM {user_relationships} WHERE rtid = 3 ORDER BY rid ASC ";
  $relationships = db_query($sql);

  while ($relationship = db_fetch_object($relationships)) { // for each relationship...

    // load the requester
    $requester = user_load($relationship->requester_id);

    // load the requestee
    $requestee = user_load($relationship->requestee_id);

    dsm("Processing relationship between requester ($requester->name) and requesteee ($requestee->name).");

    // load the requester's content profile node
    $requester_content_profile = node_load($requester->content_profile->nid);

    // load the requestee's content profile node
    $requestee_content_profile = node_load($requestee->content_profile->nid);

    if (!$requester_content_profile || !$requestee_content_profile) { continue; } 

    // add the requestee's uid to the requester's content profile user reference field array, if it isn't already there
    $found_it = false;
    if (!empty($requester_content_profile->field_dg_profile_favorites)) {
      foreach ($requester_content_profile->field_dg_profile_favorites as $favorite) {
        if ($favorite['uid'] == $requestee->uid) { $found_it = true; break; } 
      }
    }
    else { $requester_content_profile->field_dg_profile_favorites = array(); }
    if (!$found_it) {
      drupal_set_message("Adding $requestee->name to $requester->name's favorites.");
      $requester_content_profile->field_dg_profile_favorites[] = array('uid' => $requestee->uid);  
      node_save($requester_content_profile);
    }
    else {
      drupal_set_message(l($requestee->name,"user/$requestee->uid") . " was already in $requester->name's favorites.");
    }

    // add the requester's uid to the requestee's content profile user reference field array, if it isn't already there
    $found_it = false;
    if (!empty($requestee_content_profile->field_dg_profile_favorites)) {
      foreach ($requestee_content_profile->field_dg_profile_favorites as $favorite) {
        if ($favorite['uid'] == $requester->uid) { $found_it = true; break; } 
      }
    }
    else { $requestee_content_profile->field_dg_profile_favorites = array(); }
    if (!$found_it) {
      drupal_set_message("Adding $requester->name to $requestee->name's favorites.");
      $requestee_content_profile->field_dg_profile_favorites[] = array('uid' => $requester->uid);
      node_save($requestee_content_profile);
    }
    else {
      drupal_set_message(l($requester->name,"user/$requester->uid") . " was already in $requestee->name's favorites.");
    }

  } // end for each relationship

  return $html;

}