Manager Customization: Preserve the Top Menu Order

Learn how to ensure your customized top menu order stays the way you want—even after upgrading MODX.

By Bob Ray  |  December 28, 2021  |  2 min read
Manager Customization: Preserve the Top Menu Order

This is the final article in a three-part series on MODX Manager customization (click here for the first and here for the second). There are a variety of reasons to customize the back-end Manager UI for your users or groups. These include making frequently used items more accessible, preventing access to things that could cause problems, protecting confidential data, or removing things they should never see in the first place.


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 needing a dropdown 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 downside 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 a menu item changes between the time you save your menus and the time you restore them. (It’s necessary to use the text because menu items have no ID). 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:

<?php
/* SaveMenu snippet */
$output = '';
$displayOutput = '';

/* Get all menu items */
$menus = $modx->getCollection('modMenu');

/* Create the menu array for the file */
$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    ),";

    $displayOutput = '<br>Saved menu item: ' . $name;
}
$output .= "\n);";

/* Write menu array to the file */
$fp = fopen('saved_menus.txt', 'w');
fwrite($fp, $output);
fclose($fp);

return $displayOutput;

The code above will save all menu items as a PHP array to a file called saved_menus.txt. The spaces and new lines are to make the file contents readable for humans.

RestoreMenu Snippet

Now create a second Snippet called RestoreMenu with this code:

<?php
/* RestoreMenu snippet */
$output = '';

/* Get the menu array from the file */
include 'saved_menus.txt';

/* Loop through the array and restore the menu items */
foreach ($menus as $name => $fields) {
    $output = "### Restoring Menus";
    $menuObj = $modx->getObject('modMenu', array('text' => $name));
    if ($menuObj) {
        $menuObj->set('parent', $fields['parent']);
        $menuObj->set('menuindex', (int) $fields['menuindex']);
        if ($menuObj->save()) {
            $output .= "<br />Restored Menu: " . $name;
        } else {
            $output .= "<br> Could not save menu: " . $name;
        }
     } else {
        $output .= "<br>could not find menu: " . $name;
    }
}
return $output;

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:

[[!SaveMenu]]

Put this tag in the RestoreMenu Resource:

[[!RestoreMenu]]

Operation

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.


Bob Ray is the author of the MODX: The Official Guide and dozens of MODX Extras including QuickEmail, NewsPublisher, SiteCheck, GoRevo, Personalize, EZfaq, MyComponent and many more. His website is Bob’s Guides. It not only includes a plethora of MODX tutorials but there are some really great bread recipes there, as well.