Is it possible to add a page to a menu during migration? I have a very simple example here, which creates a few pages with the Migrate Plus module, but is it possible to also add them to a menu, like fx "Main navigation" (main)? The order isn't super important: https://www.drupal.org/files/issues/migrate_plus.migration.default_pages...

Comments

ressa created an issue. See original summary.

mikeryan’s picture

Project: Migrate Plus » Drupal core
Version: 8.x-2.x-dev » 8.2.x-dev
Component: Miscellaneous » migration system
Category: Task » Support request
Status: Active » Postponed (maintainer needs more info)

Moved to the core queue, not really anything to do with migrate_plus...

I'm not that conversant with menu migration, but I can tell you it's separate from node migration. See http://cgit.drupalcode.org/drupal/tree/core/modules/menu_link_content/mi... for how it's done in the Drupal-to-Drupal case.

ressa’s picture

Thanks for the tip @mikeryan. I had hoped it was just a matter of defining link name, menu name and perhaps weight. I'll have a look into the example page you linked to.

mikeryan’s picture

Status: Postponed (maintainer needs more info) » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

ressa’s picture

Here is an example of a simple menu migration I made, called migrate_plus.migration.menu_link_main.yml:

# Import menu links to default pages, like About us.
id: custom_migrate_menu_link_main
migration_group: null
label: 'Create main menu links'
source:
  plugin: embedded_data
  data_rows:
    -
      id: 2
      title: 'About'
      menu_name: 'main'
      link: 'about'
      weight: 2
      expanded: 0
    -
      id: 3
      title: 'Contact'
      menu_name: 'main'
      link: 'contact'
      weight: 3
      expanded: 1
  ids:
    id:
      type: integer
process:
  bundle:
    plugin: default_value
    default_value: menu_link_content
  title: title
  menu_name: menu_name
  link/uri:
    plugin: link_uri
    source:
      - link
  weight: weight
  expanded: expanded
  link/options: '@opts'
destination:
  plugin: entity:menu_link_content

You can create the pages with the file migrate_plus.migration.default_pages.yml:

# Import of default pages, like About us and Suggestions.
id: custom_migrate_default_pages
migration_group: null
label: 'Import default pages'
source:
  plugin: embedded_data
  ids:
    title:
      type: string
  data_rows:
    - title: 'About'
      body: |
        <p>About us text.</p>
    - title: 'Contact'
      body: |
        <p>Contact us text.</p>
destination:
  plugin: entity:node
process:
  type:
    plugin: default_value
    default_value: page
  uid:
    plugin: default_value
    default_value: 1
  title: title
  'body/value': body
  'body/format':
    plugin: default_value
    default_value: 'basic_html'
  created: created
agerard’s picture

I know this is old, but I'm curious about the last post (#6) - should that code actually work? I have a migration that successfully brings in taxonomy terms and creates content from a json export of fields (from non-Drupal content items). I'd like to also create the menu entries, like [/thisContentType/thisName] where thisContentType could be a string, yaml constant, or injected into the source json, which already contains thisName for each item. It's been difficult to find a general overview of how the menu migration process might work, and I am trying to figure out, by slowly-accreting knowledge from posts buried in various places, how to write yaml that could do this by using some combo of default_value or constant (for a sub-path) and pageID to create them directly thru a dependent migration yaml. I'm still not clear on which menu fields must even be populated. Based on this example (except that the source is json rather than internal), nothing I've tried works, and the various errors I've gotten aren't helpful.

I can also try using a script to create/specify menu links in the json before import, but as everything I've tried so far is just guesswork and I don't know where it's failing, I may end well up at the same dead end so I thought I'd ask...

quietone’s picture

@agerard, this issue is closed for three years. To get support I suggest opening a new Support Request issue or, alternatively, join the #migration channel in Drupal Slack. There are many folk there that can help.

Good luck!

dylan_plaster’s picture

I am commenting here as it is one of the first (and only) relevant results I was able to find when searching for importing/migrating menus into Drupal from a non-Drupal source. I was unable to find any good information despite hours of researching. Here is the migration I was able to come up with which successfully migrated in both nodes AND menu links, while also rendering the active trail and menu display properly. An example of how to successfully migrate menu links into a drupal 9 site:

Create two csv files. the first one is for Node and has columns like so:
Nodes.csv:
TabID | TabName | TabPath | (any other columns you need like body content, etc)

Links.csv:
TabID | alias | TabName | parentId

Where parentID points to the parent of a menu link in a hierarchy, and the 'alias' column is the same as the TabPath column from the Nodes CSV.

Now that the CSVs are configured, this yml will import the menu links (after the nodes are imported first):

# Need to do migration lookup process plugin to reference your migration
source:
  plugin: csv
  path: /path/to/your/file.csv
  delimiter: ','
  enclosure: '"'
  header_offset: 0
  ids:
    - TabID
  fields:
    0:
      name: TabID
      label: 'Unique Id'
    1:
      name: alias
      label: 'URL Alias'
    2:
      name: TabName
      label: 'Tab Name'
    3:
      name: parentID
      label: 'Parent ID'
  constants:
    slash: 'entity:node/'
process:
  id: 
    plugin: migration_lookup
    migration: name_of_migration
    source:
      - TabID
  title: TabName
  description: description
  path/pathauto: 
   plugin: default_value
   default_value: 0 # Disable pathauto
  menu_name: 
    -
      plugin: default_value
      default_value: 'test-menu-2'
  'link/uri':
      -
        plugin: concat
        source:
          - constants/slash
          - '@id'
      -
        plugin: link_uri
  'link/options': options
  route:
    -
      plugin: concat
      source:
        - constants/slash
        - '@id'
    -
      plugin: route
  parent:
    plugin: menu_link_parent
    source:
      - parentID
      - '@test-menu'
      - parent_link_path
  external:
    plugin: default_value
    default_value: 0
  expanded:
    plugin: default_value
    default_value: 0
  enabled:
    plugin: default_value
    default_value: 1
  langcode:
    plugin: default_value
    default_value: 'en'
  changed: updated
destination:
  plugin: 'entity:menu_link_content'
  no_stub: true
migration_dependencies: null

remember to replace the path with the path to your csv. Hopefully this saves someone hours of trial and error like me