Preserving Your MODX Top Menu Structure

Two utility snippets that save and restore the structure of a customized MODX Top Menu

It's relatively easy to modify the MODX Manager's Top Menu Structure. Maybe you've put the options in a different order so the ones you use often are at the top. Or maybe you've moved some subitems to the main menu bar so you can get at them without dropping down a menu.

I've written about how to do this elsewhere, but mainly it's just a matter of going to System | Actions on the Top Menu and dragging things around in the right-hand tree.

The down side of doing this is that whenever you upgrade MODX, the options will go back to their default locations. Here's an easy way to save and restore the menuindex and parent fields of your menu items.

This method uses a pair of utility snippets, one to save, and another to restore the parent and menuindex fields. One word of warning: the method will fail if the text of the menu item changes between the time you save your menus and the time you restore them. Nothing will be harmed if this happens (and it's not likely to). Any menu items with changed text just won't be reset to where you had them when you saved the menus.

SaveMenu Snippet

Create a snippet called SaveMenu with this code:

/* SaveMenu snippet */
$menus = $modx->getCollection('modMenu');

$output = "\$menus = array(\n";
foreach ($menus as $menu) {
    $name = $menu->get('text');
    $parent = $menu->get('parent');
    $menuindex = $menu->get('menuindex');

    $output .= "\n    \$" . $name . "= array(\n";
    $output .= "\n        'parent' => " . "'" . $parent . "',\n";
    $output .= "\n        'menuindex' => " . "'" . $menuindex . "',\n";
    $output .= "\n    ),";

$output .= "\n);";

$fp = fopen('saved_menus.txt', 'w');
fwrite($fp, $output);

RestoreMenu Snippet

Now create a second snippet called RestoreMenu with this code:

/* RestoreMenu snippet */
include 'saved_menus.txt';

foreach ($menus as $name => $fields) {
    $output = "\n<h3>Restoring Menus</h3>";
    $menuObj = $modx->getObject('modMenu', array('text' => $name));
    if ($menuObj) {
        $menuObj->set('parent', $fields['parent']);
        $menuObj->set('menuindex', (integer) $fields['menuindex']);
        if ($menuObj->save()) {
            $output .= "\n<br />Restored Menu: " . $name;
        } else {
            $output .= "\n<br /> Could not save menu: " . $name;
     } else {
        $output .= "\n<br />could not find menu: " . $name;

SaveMenu and RestoreMenu Resources

Next, create two resources called SaveMenu and RestoreMenu. You'll probably want them hidden from menus, and if you're the admin Super User, there's no need to publish them. Put this tag in the SaveMenu Resource:


Put this tag in the RestoreMenu Resource:



Before upgrading MODX, open up the SaveMenu Resource in the manager and click on the "View" button at the upper right. That will save your menu structure

After the upgrade, do the same with the RestoreMenu Resource. That should put your menu back the way you like it.

Comments (0)

Please login to comment.