We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 44870
    • 35 Posts
    Hi, is there a way to export Top Menu Actions setup?
    I prefer to use custom menu structure. After modx upgrade menu items comes to their standard position.
    I would be grateful for help.
      • 3749
      • 24,544 Posts
      I'm not sure it's completely safe, but you might export the modx_menus table in PhpMyAdmin before the upgrade and import it after the upgrade. I would back up the whole database first. The down side is that if there are any new menu items in the upgrade, you would probably lose them.

      A safer method would be a pair of utility snippets that just save and restore the parent and menuindex fields (and permissions, if you've modified them). That would assume that the action ID doesn't change in the upgrade, but I don't think it ever has.

      That would look something like this (totally untested):

      <?php
      /* ExportMenus */
      $menus = $modx->getCollection('modMenu');
      
      $display = '<h3>Saving ' . count($menus) . ' Menu Items</h3>';
      $output = "<?php\n\$menus = array(";
      foreach ($menus as $menu) {
          $name = $menu->get('text');
          $display .= "\n<br />Saving Menu: " . $name;
          $parent = $menu->get('parent');
          $menuindex = $menu->get('menuindex');
      
          $output .= "\n    '" . $name . "' => array(";
          $output .= "\n        'parent' => " . "'" . $parent . "',";
          $output .= "\n        'menuindex' => " . "'" . $menuindex . "',";
          $output .= "\n    ),";
      }
      $output .= "\n);";
      $output .= "return \$menus;";
      
      $file = MODX_CORE_PATH . 'cache/saved_menus.php';
      $fp = fopen($file, 'w');
      if ($fp) {
          if (fwrite($fp, $output)) {
              $display .= "\n<br /><br />Finished!";
          }
          fclose($fp);
      } else {
          $display .= "\n<br /> Could not open " . $file . ' for writing';
      }
      
      return $display;
      


      <?php
      /* ImportMenus */
      $file = MODX_CORE_PATH . 'cache/saved_menus.php';
      $menus = include $file;
      
      $output = "\n<h3>Restoring " . count($menus) . " Menu Items</h3>";
      
      foreach ($menus as $name => $fields) {
      
          $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 Item: " . $name;
              } else {
                  $output .= "\n<br /> Could not save Menu Item: " . $name;
              }
          } else {
              $output .= "\n<br />could not find Menu Object: " . $name;
          }
      }
      $output .= "\n<br /><br />Finished!";
      
      return $output;


      Let me know if it works. wink [ed. note: BobRay last edited this post 13 years, 1 month ago.]
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 44870
        • 35 Posts
        Hi, I tried second method with no success.
        After calling Export snippet there was no response.
        After calling Import snippet response was:
        $menus = array( $dashboard= array( 'parent' => '', 'menuindex' => '2', ), $dashboards= array( 'parent' => 'dashboard', 'menuindex' => '4', ), $preview= array( 'parent' => '', 'menuindex' => '1', ), $refresh_site= array( 'parent' => 'dashboard', 'menuindex' => '8', ), $remove_locks= array( 'parent' => 'system', 'menuindex' => '5', ), $search= array( 'parent' => 'site', 'menuindex' => '0', ), $new_document= array( 'parent' => 'dashboard', 'menuindex' => '7', ), $new_weblink= array( 'parent' => 'site', 'menuindex' => '1', ), $new_symlink= array( 'parent' => 'site', 'menuindex' => '2', ), $new_static_resource= array( 'parent' => 'site', 'menuindex' => '3', ), $logout= array( 'parent' => 'site', 'menuindex' => '4', ), $components= array( 'parent' => '', 'menuindex' => '3', ), $security= array( 'parent' => 'dashboard', 'menuindex' => '1', ), $user_management= array( 'parent' => 'security', 'menuindex' => '5', ), $user_group_management= array( 'parent' => 'security', 'menuindex' => '0', ), $resource_groups= array( 'parent' => 'security', 'menuindex' => '1', ), $form_customization= array( 'parent' => 'security', 'menuindex' => '2', ), $flush_access= array( 'parent' => 'security', 'menuindex' => '3', ), $flush_sessions= array( 'parent' => 'security', 'menuindex' => '4', ), $tools= array( 'parent' => 'dashboard', 'menuindex' => '2', ), $import_resources= array( 'parent' => 'tools', 'menuindex' => '0', ), $import_site= array( 'parent' => 'tools', 'menuindex' => '1', ), $propertysets= array( 'parent' => 'tools', 'menuindex' => '2', ), $sources= array( 'parent' => 'tools', 'menuindex' => '3', ), $reports= array( 'parent' => 'site', 'menuindex' => '5', ), $site_schedule= array( 'parent' => 'reports', 'menuindex' => '0', ), $view_logging= array( 'parent' => 'reports', 'menuindex' => '1', ), $eventlog_viewer= array( 'parent' => 'reports', 'menuindex' => '2', ), $view_sysinfo= array( 'parent' => 'reports', 'menuindex' => '3', ), $about= array( 'parent' => 'reports', 'menuindex' => '4', ), $system= array( 'parent' => 'dashboard', 'menuindex' => '0', ), $manage_workspaces= array( 'parent' => 'dashboard', 'menuindex' => '3', ), $system_settings= array( 'parent' => 'system', 'menuindex' => '0', ), $lexicon_management= array( 'parent' => 'system', 'menuindex' => '2', ), $content_types= array( 'parent' => 'system', 'menuindex' => '1', ), $contexts= array( 'parent' => 'dashboard', 'menuindex' => '6', ), $edit_menu= array( 'parent' => 'system', 'menuindex' => '3', ), $namespaces= array( 'parent' => 'system', 'menuindex' => '4', ), $profile= array( 'parent' => '', 'menuindex' => '0', ), $support= array( 'parent' => 'dashboard', 'menuindex' => '5', ), $forums= array( 'parent' => 'support', 'menuindex' => '0', ), $wiki= array( 'parent' => 'support', 'menuindex' => '1', ), $jira= array( 'parent' => 'support', 'menuindex' => '2', ), $api_docs= array( 'parent' => 'support', 'menuindex' => '3', ), $gallery= array( 'parent' => 'components', 'menuindex' => '0', ), $rampart= array( 'parent' => 'components', 'menuindex' => '0', ), $bigbrother= array( 'parent' => 'components', 'menuindex' => '0', ), $batcher= array( 'parent' => 'components', 'menuindex' => '0', ), $packman= array( 'parent' => 'components', 'menuindex' => '0', ), $importx= array( 'parent' => 'components', 'menuindex' => '0', ), );

          • 3749
          • 24,544 Posts
          Doh -- my bad. It looks like the array is rendering correctly, but is interpreted as text because of the extension.

          Change the name of the output file in both snippets from saved_menus.txt to saved_menus.php (and you can delete the saved_menus.txt file if you run across it).



          The first snippet is silent, but you could add something like this at the end:

          return 'Menu Saved';
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 44870
            • 35 Posts
            Hi, I changed the file extension, but the import snippet still doesn't change menu.
            P.S.(I don't know php) If the import script is succesful, it would give output ?:
            Restored Menu: name
            Now it displays whole menu array.
              • 3749
              • 24,544 Posts
              OK, I've made some changes to the code above for both snippets and added some bells and whistles. It's now partially tested.

              Let me know what happens.

              BTW, to see the old version after the import, my may need to clear the site cache and you'll definitely need to refresh the manager page in your browser.
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 44870
                • 35 Posts
                I cleared modx cache and browser's cache after import. Still no changes.
                Today I will test it on fresh Modx install. Can You give me code with latest changes to test?
                  • 3749
                  • 24,544 Posts
                  I've corrected it in the post above. You should be able to just cut and paste it.

                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting
                    • 44870
                    • 35 Posts
                    Sorry, I didn't understand you.
                    Works perfect now - no need to refresh system or browser's cache. Thanks a lot!
                      • 3749
                      • 24,544 Posts
                      I'm glad I could help. Thanks for testing it. smiley
                        Did I help you? Buy me a beer
                        Get my Book: MODX:The Official Guide
                        MODX info for everyone: http://bobsguides.com/modx.html
                        My MODX Extras
                        Bob's Guides is now hosted at A2 MODX Hosting