Module: user.module
Module Description: Manages the user registration and login system.
User.Info
Version: version = "5.1"
Project: project = "drupal"
User. Module
// $Id: user.module,v 1.745.2.1 2007/01/29 19:08:46 dries Exp $

Issue: Broken Sort Algorithm

Test Environment:
Platform: Windows XP, XAMPP, APACHE, PHP 4.4
IDE: NuSphere PhpED Version 4.6.3 Professional
Debugger: Integrated with NuSphere

This issue relates to issues:
Issue # xxxxxx – ( profile.module ) “Improper placement of “field category groups” on new user registration page”

Observed Problem: Order of “category sections” on new user registration page is not correct.

Conditions to Test / Verify Problem exists:
Use the “admin/user/profile” page to create some new “fields” to be included with the user profile, and select the option to be “Visible in user registration form”. Add several fields such that you have several “category” groupings and several fields for each “category”. Apply weights to each field. Also select option for “Public field, content shown on profile page and on member list pages”. Example:

Log out, then click on “Create Account” to display the “../user/register” page. You will see that your “category” ( with contained fields ) are nicely displayed on the page. However, your categories will be “before” the “Account Information” section that is “system generated” for the page. This is not desirable. This also exposes the underlying problem of not being able to control placement.

Problem Cause:
The sort algorithm code in function “_user_sort” called from function “uasort()” which is called from function “_user_forms” of user.module does not work because of broken code.

Details:
The parameters ( array item ) passed into the sort compare method MAY not have the array items “#weight” and “#title”. No test is made for their presence.
The array item access method ….[weight] and ….[title] should be ….[#weight] and ….[#title] respectively.
The end result is an improper sort.

Remedy:
This is a workaround that I have created and tested to allow the sort to work properly. It is based on how the sort in “uasort” in drupal_render() works.

BEFORE:

function _user_sort($a, $b) {
  return $a['weight'] < $b['weight'] ? -1 : ($a['weight'] > $b['weight'] ? 1 : ($a['title'] < $b['title'] ? -1 : 1));
}

AFTER:

function _user_sort($a, $b) {
/**
 * Implement same technique as in Function used by uasort in drupal_render() to sort structured arrays
 * by weight and title.  Must allow for missing weights and titles
 */
  $a_weight = (is_array($a) && isset($a['#weight'])) ? $a['#weight'] : 0;
  $b_weight = (is_array($b) && isset($b['#weight'])) ? $b['#weight'] : 0;
  $a_title = (is_array($a) && isset($a['#title'])) ? $a['#title'] : '';
  $b_title = (is_array($b) && isset($b['#title'])) ? $b['#title'] : '';  
  return $a_weight < $b_weight ? -1 : ($a_weight > $b_weight ? 1 : ($a_title < $b_title ? -1 : 1));
}

patch file “user_sort.patch” is attached for review.

Note: I can provide additional information, including screen prints from debugger sessions and page output if required.

CommentFileSizeAuthor
#3 user_sort_0.patch1.14 KBchx
user_sort.patch1 KBdnuss

Comments

dnuss’s picture

This issue relates to issues:
Issue # http://drupal.org/node/124643 – ( profile.module ) “Improper placement of “field category groups” on new user registration page”

drumm’s picture

Status: Active » Needs work

Be sure to use the 'patch (...)' status for issues with patches for faster review.

Looks okay, but lets take this opportunity to remove the unreadable nested ternary operators and use an if statement for that last changed line.

The comment style you used is for function documentation and should go immediately above the function. Or use '//' comments. For more information see http://drupal.org/coding-standards.

chx’s picture

Priority: Critical » Normal
Status: Needs work » Needs review
StatusFileSize
new1.14 KB

_user_sort is used at another place, we need another function.

drumm’s picture

Status: Needs review » Fixed

Another function is not needed since this is only explicitly setting the zero weight and title to the expected values.

I backported code from Drupal 6, http://cvs.drupal.org/viewvc.py/drupal/drupal/modules/user/user.module?r..., instead of using this patch.

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.