function hook_user_register_user_attributes

6.x user.api.php hook_user_register_user_attributes()

Sample hook other modules implement to register a user attribute.

Simply return an array as illustrated below to register your module's content types.

IMPORTNAT: The attribute's machine name must be a valid variable name. No spaces or symbols.

Values are saved to the user_attributes table automatically as name__fieldname In the example below, the value would be saved under visa_status__visa

File

modules/user/user.api.php, line 22

Code

function hook_user_register_user_attributes() {

  $arr ['visa_status'] = array(
    'title' => 'Visa Status',
    'description' => 'Determines the visa status of the user.',
    'settings' => array(
      'large_profile' => TRUE,
      'large_profile_editable' => TRUE,
      'mini_profile' => TRUE,
      'mini_profile_editable' => FALSE,
      'user_type' => 'student', // Can be "student", "faculty", or "all"
    ),
  );


  $arr ['fun_field'] = array(
    'title' => 'Enter any text you like:',
    'settings' => array(
      'user_type' => 'student',
      'large_profile_editable' => TRUE,
    ),
  );


  // If we are in a popup (dialog)...
  if (@$_GET ['window_mode'] == 'popup') {
    // We want to make sure we redirect to our handler URL, which will close the dialog.
    $arr ['visa_status']['settings']['#redirect'] = array(
      'path' => 'content-dialog-handle-after-save',
      'query' => '',
    );

    $arr ['fun_field']['settings']['#redirect'] = array(
      'path' => 'content-dialog-handle-after-save',
      'query' => '',
    );


    /**
     * // To do all attributes at once...
    foreach ($arr as $attr => $details) {
      $arr[$attr]['settings']['#redirect'] = array(
        'path' => 'content-dialog-handle-after-save',
        'query' => '',        
      );      
    }
     */

  }


  $fields = array();

  $fields ['visa'] = array(
    'type' => 'select',
    'label' => 'Status:',
    'options' => array(
      'open' => t('Open'),
      'closed' => t('Closed'),
    ),
    'required' => TRUE,
    'hide_please_select' => TRUE,
    'weight' => 20,
  );


  $arr ['visa_status']['fields'] = $fields;

  // ** required! **
  // Display settings
  $arr ['visa_status']['display']['visa'] = array(
    'label' => 'Visa Status:',
    'value' => 'Selected: <i>@value</i>', // We can make use of HTML and replacement patterns.  
// @value means whatever was selected / typed in.  @key means they key from an options field.
// If 'value' is ommitted, we display @value by default.
  );


  //////////////////////////////////////////////////

  $fields = array();

  $fields ['fun_field_val'] = array(
    'type' => 'textfield',
    'label' => 'Fun Field:',
  );

  $arr ['fun_field']['display']['fun_field_val'] = array(
    'label' => 'Fun Field:',
  );

  $arr ['fun_field']['fields'] = $fields;

  return $arr;

}