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);
}

Comments

alex8810’s picture

I was trying to integrate the Name module with the RealName module, but I can't get it to work. The radio button for the name module appears under the modules tab, but under the fields tab, the fields are all brought in from Content Profile. What do I need to do?

NancyDru’s picture

To start with, move this to the issue queue.

The Name module is a CCK field module. If you use it to provide a name field within a Content Profile node type, I would think it would work.

alex8810’s picture

I did do that. The name field just will not show up under the fields tab for some reason.

NancyDru’s picture

I saw the issue; let's discuss it there, please.

tutumlum’s picture

What if the custom module keeps name fields in a straight table line like:

uid                  name                  surname
----                 -------              ----------
1                    Bob                    Marta
2                    Nichole                Kidney

NancyDru’s picture

It only supports Profile and Content Profile. But you can add support by looking at how those are done.