HOWTO: Create menu_link_content items with a parent

Created on 3 May 2025, 15 days ago

While working on a site I changed from using Default Content's drush commands to export the entities to doing them by hand based upon examples previously created. This worked well up until I was trying to set the parent on menu_link_content entities. After some testing I finally worked out how to do it so wanted to document it here.

This is the parent item that will have child element(s) later:

_meta:
  version: '1.0'
  entity_type: menu_link_content
  uuid: 2d0659d1-ba09-4bca-9adc-9833d484590a
  bundle: main
  default_langcode: en
default:
  enabled:
    -
      value: true
  title:
    -
      value: "The parent"
  menu_name:
    -
      value: main
  link:
    -
      uri: 'internal:/some/thing'
      title: ''
  external:
    -
      value: false
  rediscover:
    -
      value: false
  weight:
    -
      value: -10
  expanded:
    -
      value: false
  revision_translation_affected:
    -
      value: true

This is the child element:

_meta:
  version: '1.0'
  entity_type: menu_link_content
  uuid: e412fa5d-1926-428d-8926-45f257d9c92e
  bundle: main
  default_langcode: en
  depends:
    2d0659d1-ba09-4bca-9adc-9833d484590a: menu_link_content
default:
  enabled:
    -
      value: true
  title:
    -
      value: "Child page"
  menu_name:
    -
      value: main
  link:
    -
      uri: 'internal:/over/there'
      title: ''
  external:
    -
      value: false
  rediscover:
    -
      value: false
  weight:
    -
      value: -100
  expanded:
    -
      value: false
  revision_translation_affected:
    -
      value: true
  parent:
    -
      value: 'menu_link_content:2d0659d1-ba09-4bca-9adc-9833d484590a'

The important pieces of the child are:

  • The parent's UUID must be listed as a "depends" line to make sure the parent is created first.
  • The "parent" value is set to "menu_link_content:[PARENTUUID]" i.e. the entity type name of "menu_link_content" followed by the parent item's UUID. This matches what is stored as the "parent" value of the {menu_link_content_data} table, and also the "parent" value of the {menu_tree} table.

I tried using the parent item's ID, its UUID, using "target_id" as the key, using "parent_id" as the attribute, lots of different options, but this was the correct combination in the end.

πŸ“Œ Task
Status

Active

Version

2.0

Component

Documentation

Created by

πŸ‡ΊπŸ‡ΈUnited States DamienMcKenna NH, USA

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024