Preserving Your MODX Top Menu Structure

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:

[code language="""php"""]<br /><?php<br />/* SaveMenu snippet */<br />$menus = $modx->getCollection('modMenu');<br /><br />$output = "\$menus = array(\n";<br />foreach ($menus as $menu) {<br /><%%KEEPWHITESPACE%%>    $name = $menu->get('text');<br /><%%KEEPWHITESPACE%%>    $parent = $menu->get('parent');<br /><%%KEEPWHITESPACE%%>    $menuindex = $menu->get('menuindex');<br /><br /><%%KEEPWHITESPACE%%>    $output .= "\n    \$" . $name . "= array(\n";<br /><%%KEEPWHITESPACE%%>    $output .= "\n        'parent' => " . "'" . $parent . "',\n";<br /><%%KEEPWHITESPACE%%>    $output .= "\n        'menuindex' => " . "'" . $menuindex . "',\n";<br /><%%KEEPWHITESPACE%%>    $output .= "\n    ),";<br /><br />}<br />$output .= "\n);";<br /><br />$fp = fopen('saved_menus.txt', 'w');<br />fwrite($fp, $output);<br />fclose($fp);<br />[/code]

RestoreMenu Snippet

Now create a second snippet called RestoreMenu with this code:

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

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:

[code language="""html"""]<br />[[SaveMenu]]<br />[/code]

Put this tag in the RestoreMenu Resource:

[code language="""html"""]<br />[[RestoreMenu]]<br />[/code]


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.

For more information on how to use MODX to create a web site, see my web site Bob’s Guides, or better yet, buy my book: MODX: The Official Guide.

Looking for quality MODX Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , | Posted under MODX, MODX | RSS 2.0

Author Spotlight

Bob Ray

Bob Ray is the author of MODX: The Official Guide and over 30 MODX add-on components. He hosts Bob's Guides, a source of valuable information for MODX users, and has been very active in the MODX Forums with over 19,000 posts.

Leave a Reply

Your email address will not be published. Required fields are marked *