RealName: Supporting an Alternate Module

Last updated on
30 April 2025

The Real Name module can be extended to support other modules that may supply data to Real Name. This capability was designed to be somewhat flexible.

Specifying a Module

The first task is to tell Real Name that another module is available and whether or not it can supply more than one content type. This is done in the "realname_supported.inc" file.

// $Id: realname_supported.inc,v 1.1.2.2 2008/11/03 20:10:34 nancyw Exp $
/**
 * @file
 * Realname module support list.
 */

function realname_supported_modules() {
  $list = array(
    'content_profile' => array(
      'name' => 'Content Profile',
      'types' => TRUE,
      ),
    'profile' => array(
      'name' => 'Core Profile',
      'types' => FALSE,
      ),
    );
  return $list;
}

This function returns an array that is keyed by the internal module name and contains a "name" part that is the external module name, and a "types" part that indicates whether the modules supports multiple content types.

That was the easy part. Now, we have to build the support pieces.

Support Functions

All of the support functions are included in a file called "realname_mymodule.inc" where mymodule is the internal module name for which you are building support.

// $Id: realname_content_profile.inc,v 1.1.2.2 2008/11/03 20:11:09 nancyw Exp $
/**
 * @file
 * Realname module support for Content Profile module.
 */

/**
 * Implementation of hook_profile_load();
 */
function content_profile_load_profile(&$account, $type = NULL) {
  $profile = content_profile_load($type, $account->uid);
  if (!$profile) {
    return;
  }
  $fields = content_fields(NULL, $type);
  foreach ($fields as $field_name => $field_attributes) {
    $values = array();
      $contents = $profile->$field_name;
      foreach ($contents as $content) {
        if (isset($content['value'])) {
          $values[] = $content['value'];
        }
        else {
          $values[] = content_format($field_name, $content);
        }
      }
    if (empty($account->{$field_name})) {
      switch (count($values)) {
        case 0:
          $account->{$field_name} = NULL;
          break;
        case 1:
          $account->{$field_name} = $values[0];
          break;
        default:
          $account->{$field_name} = $values;
      }
    }
  }
}

function realname_content_profile_get_types() {
  return content_profile_get_types('names');
}

function realname_content_profile_get_fields($current) {
  $fields = $links = array();
  $all_fields = content_fields(NULL, $type);
  foreach ($all_fields as $field_name => $field_attributes) {
    switch ($field_attributes['type']) {
      case 'text':
        if (!$field_attributes['multiple']) {
          $selected = array_key_exists($field_name, $current);
          $fields[$field_name] = array(
            'title' => $field_attributes['widget']['label'],
            'weight' => $selected ? $current[$field_name] : 0,
            'selected' => $selected,
            );
        }
        break;

      case 'link':
        $links[$field_name] = $field_attributes['widget']['label'];
    }
  }
  return array('fields' => $fields, 'links' => $links);
}

mymodule_load_profile

If the module you are supporting does not have a "mymodule_load_profile" function it must be added here.

This function must merge its values into the supplied object (usually a user object, hence it is called "$account").

realname_mymodule_get_types

This function returns an array specifying the available content types. It is keyed by the internal content type name and contains the external content type name. For example it might return array('mytype' => 'My Content Type').

realname_mymodule_get_fields

This function provides an array of the available fields and their descriptors. The function is provided with an array containing the currently used fields. The RealName module expects to get back an array containing two arrays. The first is called "fields" and is keyed by the internal field name and containing three elements:

  • title - the external name (or "label") for the field
  • weight - this is the weight that positions the fields in the derived RealName. The current value is provided in the supplied list.
  • selected - this is a boolean that indicates whether or not the field is currently in use.

The second part of the returned array is called "links" and is ued to supply the "homepage" possibilities.

Do I Need All That?

Not necessarily, as you can see in the support for the core Profile module below. In the case of the core Profile module, it already provides a "profile_load_profile." Additionally, since it does not allow multiple content types, the "_get_types" function is not needed.

// $Id: realname_profile.inc,v 1.1.2.2 2008/11/03 20:11:09 nancyw Exp $
/**
 * @file
 * Realname module support for Profile (core) module.
 */

function realname_profile_get_fields($current) {
  $fields = $links = array();
  $result = db_query("SELECT * FROM {profile_fields}");
  while ($field = db_fetch_array($result)) {
    switch ($field['type']) {
      case 'textfield':
        $name = $field['name'];
        $selected = array_key_exists($name, $current);
        $fields[$name] = array(
          'title' => $field['title'],
          'weight' => $selected ? $current[$name] : 0,
          'selected' => $selected,
          );
        break;

      case 'url':
        $links[$field['name']] = $field['title'];
        break;

      default:
        break;
    }
  }
  return array('fields' => $fields, 'links' => $links);
}

Help improve this page

Page status: Not set

You can: