Commit 7dc544a6 authored by TravisCarden's avatar TravisCarden
Browse files

Implemented weight sorting.

parent a1555d71
...@@ -44,9 +44,9 @@ function checklistapi_get_checklist_info() { ...@@ -44,9 +44,9 @@ function checklistapi_get_checklist_info() {
$checklists = &drupal_static(__FUNCTION__); $checklists = &drupal_static(__FUNCTION__);
if (!isset($checklists)) { if (!isset($checklists)) {
$checklists = module_invoke_all('checklistapi_checklist_info'); $checklists = module_invoke_all('checklistapi_checklist_info');
// checklistapi_prepare_checklists($checklists); checklistapi_sort_array($checklists);
drupal_alter('checklistapi_checklist_info', $checklists); drupal_alter('checklistapi_checklist_info', $checklists);
// checklistapi_prepare_checklists($checklists); checklistapi_sort_array($checklists);
} }
return $checklists; return $checklists;
} }
...@@ -112,38 +112,43 @@ function checklistapi_permission() { ...@@ -112,38 +112,43 @@ function checklistapi_permission() {
} }
/** /**
* Sorts checklist components by weight. * Recursively sorts array elements by weight.
* *
* @param array $checklists * @param array $array
* A multidimensional array of checklist definitions returned from * A nested array of elements and properties such as the checklist definitions
* hook_checklistapi_checklist_info(). * returned by hook_checklistapi_checklist_info().
* *
* @see checklistapi_get_checklist_info() * @see checklistapi_get_checklist_info()
*/ */
function checklistapi_prepare_checklists(&$checklists) { function checklistapi_sort_array(&$array) {
if (!empty($checklists)) { $child_keys = element_children($array);
// Sort checklists by weight. if (count($child_keys)) {
uasort($checklists, 'element_sort'); $incrementer = 0;
// Loop through checklists. $children = array();
foreach ($checklists as $checklist_id => $checklist) { foreach ($child_keys as $key) {
if (!empty($checklist['groups']) && is_array($checklist['groups'])) { // Move child to a temporary array for sorting.
// Sort groups by weight. $children[$key] = $array[$key];
uasort($checklists[$checklist_id]['groups'], 'element_sort'); unset($array[$key]);
// Loop through groups. // Supply a default weight if missing or invalid.
foreach ($checklist['groups'] as $group_id => $group) { if (empty($children[$key]['#weight']) || !is_numeric($children[$key]['#weight'])) {
if (!empty($group['items']) && is_array($group['items'])) { $children[$key]['#weight'] = 0;
// Sort checklist items by weight. }
uasort($checklists[$checklist_id]['groups'][$group_id]['items'], 'element_sort'); // Increase each weight incrementally to preserve the original order when
// Loop through checklist items. // not overridden. This accounts for undefined behavior in PHP's uasort()
foreach ($group['items'] as $item_id => $item) { // function when its comparison callback finds two values equal.
if (!empty($item['links']) && is_array($item['links'])) { $children[$key]['#weight'] += ($incrementer++ / 1000);
// Sort links by weight. // Descend into child.
uasort($checklists[$checklist_id]['groups'][$group_id]['items'][$item_id]['links'], 'element_sort'); checklistapi_sort_array($children[$key]);
} }
} // Sort by #weight.
} uasort($children, 'element_sort');
} // Remove incremental weight hack.
} foreach ($children as $key => $child) {
} if ($key == 'i_suck') {
$children[$key]['#weight'] = round($children[$key]['#weight']);
}
}
// Put children back in the main array.
$array += $children;
} }
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment