The following plugin...
...reorganizes the menuindex after saving so that a document with menuindex just set to [tt]n[/tt] really appears at [tt]n[/tt]th position in menu.
...stops auto-incrementing menuindex if all childs have set it to 0 (for alphabetical sortings like [tt]ORDER BY menuindex, pagetitle DESC[/tt]).
It’s written from scratch, bad code, but hopefully useful for someone
global $content;
$e = &$modx->Event;
switch($e->name) {
case 'OnDocFormPrerender':
if(($_REQUEST['pid'] > 0) && ($id == 0)) {
$query = $modx->db->query("SELECT MAX(menuindex) FROM {$modx->db->config['table_prefix']}site_content WHERE parent = {$_REQUEST['pid']}");
$menuindex = intval($modx->db->getValue($query));
if($menuindex>0) {
$content['menuindex'] = $menuindex++;
} else {
$content['menuindex'] = 0;
}
}
break;
case 'OnDocFormSave':
$query = $modx->db->query("SELECT parent, menuindex FROM {$modx->db->config['table_prefix']}site_content WHERE id = {$id}");
if($current = $modx->db->getRow($query)) {
$query = $modx->db->query("SELECT id FROM {$modx->db->config['table_prefix']}site_content WHERE parent = {$current['parent']} AND menuindex > 0 ORDER BY menuindex ASC, editedon DESC");
$i = 1;
while($sister = $modx->db->getRow($query)) {
$update = $modx->db->query("UPDATE {$modx->db->config['table_prefix']}site_content SET menuindex = {$i} WHERE id = {$sister['id']}");
$i++;
}
}
break;
default:
return;
break;
}
To use it, create a new plugin, paste the code and select OnDocFormPrerender and OnDocFormSave checkboxes in System Events Tab.