Import And Export Menus With Menu Import Module In Drupal 7

Menu Import allows site builders to import and export menu structures using a simple JSON style notation. The module helps site builders to create, test and deploy menu structures without having to re-enter in menu items over and over again – you will save a lot of time using this module.

Here is an example, if you were to import the text below, you’ll end up with a menu structure that represents the order and depth of the text.

Home
News
- Drupal
-- Drupal 7
-- Drupal 6
- PHP
Contact
- About Us

Fig 1.0

If you need a menu item to point to an existing page or node, simply add {"url":"contact"} after the menu name on the same line. For example, Contact {"url":"contact"} will create a menu item called Contact and set the path to /contact. When a URL is not specified, then the menu item URL is automatically set to <front> (homepage).

In this tutorial, we’ll look at how you can import and export menus using the Menu Import UI and via Drush.

Getting Started

Download and install Menu Import module.

If you use Drush run the following commands:

drush dl menu_import
drush en menu_import

How To Import Menu Items

The first piece of functionality we’ll look at is how to import menus into a Drupal site. As stated earlier, Menu Import uses a JSON style notation. This makes it really easy for site builders to experiment with different menu structures.

You can indent menus with either a “*” or “-” character there is no difference between one or the other.

How To

1. Go to Structure -> Menus (admin/structure/menu) and click on the “Import menu” tab.

Fig 1.1

2. Select “Main menu” as the “Target menu” drop-down list.

3. In the Source section, click on “Text import” and paste the text below into the text area.

If you have a large menu file, use the “Menu file” file field instead.

Home
News
- Drupal
-- Drupal 7
-- Drupal 6
- PHP
Contact
- About Us

Fig 1.2

4. In the Options section check the “Remove existing menu items” checkbox.

Fig 1.3

5. Once you are ready, click on the “Upload & preview” button.

6. At this point, the menus have not been imported. You must preview the changes before the import. Once you are happy click on the Import button.

Fig 1.4

7. Once the import process has completed, you’ll be redirected to the “Main menu” listing page with a status of the import.

Fig 1.5

As you can see, importing menus are really easy. There are just three processes:

  1. Define menu structure
  2. Preview menu import
  3. Import menus

How To Export Menu Items

In the previous section we looked at how to import menus into a site, now let’s look at how to export menus out of a Drupal site.

How To

1. Go to Structure -> Menus (admin/structure/menu) and this time click on the “Export menu” tab.

Fig 1.6

2. Select which menu set you want to export from the “Menu to export” drop-down list, and choose which line ending you want to use from the “Line ending” drop-down list, and then click on the Export button.

Drush Integration

The final functionality I want to demonstrate is Menu Import’s integration with Drush.

Here is the command for importing menus from a text file called “main-menu-export.txt”.

drush menu-import main-menu-export.txt main-menu

If you need existing menus to be deleted, then add --clean-import to the command.

drush menu-import main-menu-export.txt main-menu --clean-import

Finally, if you want to export using Drush run the example command below:

drush menu-export main-drush-export.txt main-menu --line-ending=mac

For detailed documentation check out the Drush help text.

If you have any questions, please leave a comment.

Ivan Zugec

About Ivan Zugec

Ivan is the founder of WebWash and spends most of his time consulting and writing about Drupal. He's been working with Drupal for 12 years and has successfully completed several large Drupal projects in Australia.

9 thoughts on “Import And Export Menus With Menu Import Module In Drupal 7”

    1. I haven’t had much luck with importing and exporting menus via Features. Menu Import looks very straightforward though. I’m definitely going to try it out! If others had success using Features, I’d like to hear about that as well.

      1. I don’t like using Features to manage menus. On most website that I have worked on, menus were treated as content and not configuration.

        With Menu Import, you can have non developers manage the menu structure without having to deploy a module update.

  1. I have tried with drush exporting menus and importing and it did not work.

    When exporting a menu the only a subset of the items is exported.

    When importing files generated by the GUI some items were repeated even when using the –clean-import option.

    I also tried with the dev version and it still did not work.

    Anyway this is an excelent tool. Kudos to the developers. When drush issues get fixed it will make it even greater.

  2. Hi there,

    Can you import/export different menus when you have multiple user roles with different menus?

    1. How are you controlling access? With another module?

      The “Menu Import” module is not tied to user roles, it’ll just import the menu items. Without knowing more about your use case, it’s difficult for me to give you a direct answer. 🙂

  3. Hi! I’m trying to use this module, but I get an error when I attempt to import:

    PDOException: SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column ‘title’ at row 1: INSERT INTO {node} (type, language, title, uid, status, created, changed) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6); Array ( [:db_insert_placeholder_0] => page [:db_insert_placeholder_1] => und [:db_insert_placeholder_2] => About -Superintendent’s Message -Mission-Belief-Identity -About ACS –Common Core –Block Schedule –Technology Integration -Why ACS? -Strategic Priorities -Facilities –Campus –New Complex –Learning Commons –Audio, Visual and Technology –Maker Spaces –Arts –Cafeteria and café -Contact us [:db_insert_placeholder_3] => 1 [:db_insert_placeholder_4] => 1 [:db_insert_placeholder_5] => 1458173401 [:db_insert_placeholder_6] => 1458173401 ) in drupal_write_record() (line 7351 of /srv/bindings/14e006978d1046ee8db53ea9b094b319/code/includes/common.inc).

    1. Hi Justin,

      You’re getting this error because the title is too long.

      “String data, right truncated: 1406 Data too long for column ‘title’ at row 1: “

Comments are closed.

Media Management in Drupal

Download a FREE 8 part video course on managing media in Drupal.