Nathan
Nathan

Reputation: 25006

Exporting a Menu in Drupal

I'm new to Drupal. I created a menu that I placed inside the Sidebar First Region.

I was able to place my menu in the Sidebar First Region by going to the blocks page "admin/structure/block". Is a menu seen as a block, since I am able to assign a region for a menu inside the BLOCKS page? or is just the name of the page misleading, since it can take care of both blocks and menus?

How can I import/export a menu that has a bunch of links inside it ? I want to export it from local to staging and prod. Features doesn't seem to work? I found menu Block and menu_import to say to be able to do that, but is there another easier or better way?

Upvotes: 2

Views: 3248

Answers (5)

bbu23
bbu23

Reputation: 17

There is the Menu Migration (Import & Export) module available for Drupal 10.2 and above.

The module handles hierarchies, translations and other extra fields and it provides two ways of importing and exporting menu items:

  1. Through code (menus are exported/imported to/from a directory in the Drupal Project)
  2. Through File Download/Upload

The download/upload option would match your requirements of being able to easily export menus from a local instance to a staging/prod instance.

Upvotes: 0

milkovsky
milkovsky

Reputation: 8862

If you need a one time export/import of your menus, here is a simple script to export/import menus, that you can re-use in drush php on a remote server (You can optionally export the menus content to some file in the tmp folder):

function menus_export() {
  $data = [];
  // The menus to export.
  $menus = ['footer-menu', 'main-menu'];
  foreach ($menus as $menu) {
    $menuLinkIds = \Drupal::entityQuery('menu_link_content')
      ->accessCheck(FALSE)
      ->condition('menu_name', $menu)
      ->execute();
    $menuLinks = \Drupal\menu_link_content\Entity\MenuLinkContent::loadMultiple($menuLinkIds);
    foreach ($menuLinks as $link) {
      if (!empty($link)) {
        $linkArray = $link->toArray();
        foreach ($linkArray as $key => $linkArrayItem) {
          $linkData[$key] = reset($linkArrayItem);
        }
        $data[$menu][$link->id()] = $linkData;
        unset($linkData);
      }
    }
  }
  
  // You can optionally export the menus content to some file.
  file_put_contents( 'public://tmp/menu_export.txt', '<?php return '.var_export($data, true ).";\n" );

  return $data;
}

Afterwards import the menus:

function menus_import($data) {
  foreach ($data as $menu => $links) {
    foreach ($links as $link) {
      unset($link['id'], $link['revision_id']);
      $menuLinkEntity = \Drupal::entityQuery('menu_link_content')
        ->accessCheck(FALSE)
        ->condition('uuid', $link['uuid'])
        ->execute();
      if (!$menuLinkEntity) {
        $menuLinkEntity = \Drupal\menu_link_content\Entity\MenuLinkContent::create();
      }
      else {
        $menuLinkEntity = \Drupal\menu_link_content\Entity\MenuLinkContent::load(reset($menuLinkEntity));
      }
      foreach ($link as $key => $items) {
        $menuLinkEntity->set($key, $items);
      }
      $menuLinkEntity->save();
    }
  }
}

Upvotes: 1

Nasser Ali Karimi
Nasser Ali Karimi

Reputation: 4663

My experience with Drupal 8 step by step

First of all, you need to install Menu Export module and activate it. After that, you will have Export Menu in the Structure part.

Steps to Export & Import

1- In the Export Menu page domain.com/admin/config/development/menu_export, you have three option, at Menu list select which menus you want to export.

2- On the Export section, export your menus.

(In this step menu setting will not be exported on YML file then you need to continue.)

3- Now you need to export system configuration from domain.com/admin/config/development/configuration/full/export or drush cex

Note: after export, you will have menu_export.export_data.yml file that contains your menu settings

4- Transfer configuration anywhere you want, then you need to import them from domain.com/admin/config/development/configuration/full/import or drush cim

5- Then you need to import menus to your system from UI domain.com/admin/config/development/menu_export/import

Important Note

Clear your cache

In some cases, mostly with Administration menu you need do and extra step. That you need to just open the edit Administration menu page and just saving it without any changes after this you should see your updated menu on administration as well.

Upvotes: 3

Yuseferi
Yuseferi

Reputation: 8670

There are two modules for doing that:

  1. There's also Menu Import and Export:

This module helps in exporting and importing Menu Items among cloned sites which is not possible using CMI.

This module exports the menu entity as config YAML and will be imported on subsequent instances.

  1. another one is Config Menu Link module.

Upvotes: 0

user2486435
user2486435

Reputation:

Menus are made available as blocks. Thats's why they are available in the blocks admin area. Yes, Features does not export menu items themselves as that is content and their links might have different node id's to stage or production sites.

https://www.drupal.org/project/menu_import seems to be the best solution to me for the job. You could also see if https://www.drupal.org/project/uuid_features module will do the job, but not sur

Upvotes: 1

Related Questions