Skip to content
This repository has been archived by the owner on May 13, 2018. It is now read-only.

Commit

Permalink
#517844 by voxpelli: Use hooks for realname support
Browse files Browse the repository at this point in the history
  • Loading branch information
voxpelli committed Nov 5, 2010
1 parent 49936c0 commit 5183525
Show file tree
Hide file tree
Showing 6 changed files with 331 additions and 59 deletions.
32 changes: 24 additions & 8 deletions realname.admin.inc
Expand Up @@ -215,7 +215,6 @@ function realname_admin_bypass_submit($form, &$form_state) {
function realname_admin_module($form_state) {
$form = array();
// Get the list of modules we support.
include_once(drupal_get_path('module', 'realname') .'/realname_supported.inc');
$supported_modules = realname_supported_modules();

$choices = $show_types = array();
Expand Down Expand Up @@ -274,9 +273,13 @@ function realname_admin_module_submit($form, &$form_state) {

$show_types = $form_state['values']['show_types'];
if ($show_types[$module]) {
include_once(drupal_get_path('module', 'realname') .'/realname_'. $module .'.inc');
// This module uses types, so let's see what types are allowed.
$types = call_user_func('realname_'. $module .'_get_types');
$module_info = realname_supported_modules($module);
if (isset($module_info['file'])) {
$path = !empty($module_info['path']) ? $module_info['path'] : drupal_get_path('module', $module);
require_once($path .'/'. $module_info['file']);
}
// This module uses types, so let's see what types are allowed.
$types = (array) module_invoke($module, 'realname_get_types');
if (count($types) > 1) {
$form_state['storage']['types'] = $types;
return;
Expand All @@ -297,14 +300,29 @@ function realname_admin_module_submit($form, &$form_state) {

function realname_admin_fields() {
$form = array();
$current = variable_get('realname_fields', array());
$module = variable_get('realname_profile_module', NULL);
$type = variable_get('realname_profile_type', NULL);
// Do we have a module set yet?
if (!$module) {
drupal_goto('admin/user/realname/module');
}

$module_info = $module ? realname_supported_modules($module) : NULL;
if (isset($module_info['fields']) && !$module_info['fields']) {
$form['heading'] = array(
'#type' => 'item',
'#value' => t("You are using the %module module to provide data and it doesn't utilize fields.", array('%module' => $module)),
);
return $form;
}

if (isset($module_info['file'])) {
$path = !empty($module_info['path']) ? $module_info['path'] : drupal_get_path('module', $module);
require_once($path .'/'. $module_info['file']);
}
$current = variable_get('realname_fields', array());
$type = (!isset($module_info['type']) || !$module_info['type']) ? variable_get('realname_profile_type', NULL) : NULL;
$profile_fields = (array) module_invoke($module, 'realname_get_fields', $current, $type);

$what = t('You are using the %module module to provide fields.', array('%module' => $module));
if ($type) {
$what .= t('The %type type is the source of data.', array('%type' => $type));
Expand All @@ -315,8 +333,6 @@ function realname_admin_fields() {
'#value' => $what,
);

include_once(drupal_get_path('module', 'realname') .'/realname_'. $module .'.inc');
$profile_fields = call_user_func('realname_'. $module .'_get_fields', $current, $type);
$fields = $profile_fields['fields'];
uasort($fields, '_realname_sort');
$links = $profile_fields['links'];
Expand Down
172 changes: 172 additions & 0 deletions realname.api.php
@@ -0,0 +1,172 @@
<?php
// $Id$

/**
* @file
* Hooks provided by the RealName module.
*/

/**
* @addtogroup hooks
* @{
*/

/**
* Define RealName support and options.
*
* This hook enables modules to register RealName support.
*
* @return
* An array of options that the module support. The item is an associative array
* that may contain the following key-value pairs:
* - "name": Required. The name of the module.
* - "types": Whether the module supports types.
* - "fields": Whether the module uses fields.
* - "file": A file that will be included before the module is used by RealName;
* this allows callback functions to be in separate files. The file should
* be relative to the implementing module's directory unless otherwise
* specified by the "file path" option.
* - "file path": The path to the folder containing the file specified in
* "file". This defaults to the path to the module implementing the hook.
*/
function hook_realname() {
return array(
'name' => 'Content Profile',
'types' => TRUE,
'fields' => TRUE,
'file' => 'realname_content_profile.inc',
'path' => drupal_get_path('module', 'realname'),
);
}

/**
* Loads the profile fields.
*
* @param $account
* An user object.
* @param $type
* The type used - if supported by the module.
*/
function hook_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) {
if (isset($profile->$field_name)) {
$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;
}
}
}
}
}

/**
* Gets available types.
*
* Supplies the types supported by the module if the module supports types.
*
* @return
* An array keyed by type with info.
*/
function hook_realname_get_types() {
return content_profile_get_types('names');
}

/**
* Gets available fields.
*
* Supplies the fields supported by the module if the module supports fields.
*
* @param $current
* An array with the currently used fields keyed by field.
* @param $type
* An array containing the current type.
* @return
* An associative array with two keys:
* - "fields": Required. An array keyed by field name of fields as
* associative arrays containing:
* - "title": The title of the field
* - "weight": The weight of the field
* - "selected": A boolean value of whether the field is slected or not
* - "links": Required. An array of field labels keyed by field name
*/
function hook_realname_get_fields($current, $type = NULL) {
$fields = $links = array();
$all_fields = content_fields(NULL, $type);
if ($all_fields) {
foreach ($all_fields as $field_name => $field_attributes) {
// If it's not they type we are looking for, then skip the field.
if ($field_attributes['type_name'] != $type) {
continue;
}
switch ($field_attributes['type']) {
case 'text':
if ($field_attributes['multiple']) {
drupal_set_message(t('The RealName module does not currently support fields with multiple values, such as @fld.', array('@fld' => $field_name)), 'warning');
}
else {
$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'];
}
}
}
else {
drupal_set_message(t('The !type content type has no fields to use.', array('!type' => $type)), 'error');
}

if (variable_get('realname_use_title', FALSE)) {
$fields['title'] = array(
'title' => t('Node title'),
'weight' => isset($current['title']) ? $current['title']['weight'] : 0,
'selected' => array_key_exists('title', $current),
);
}

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

/**
* Gets a name for a user
*
* Modules not supporting fields for name data has to
* implement this hook instead
*
* @param $account
* An user object.
* @return
* A string with the name or NULL.
*/
function hook_realname_make($account) {
$info = _connector_information_fetch($account);
return !empty($info['real name']) ? $info['real name'] : NULL;
}

0 comments on commit 5183525

Please sign in to comment.