Skip to content

Instantly share code, notes, and snippets.

@pingevt
Last active February 16, 2022 17:21
Show Gist options
  • Save pingevt/4cea26c7a2401e6fa71b5f69c55eb952 to your computer and use it in GitHub Desktop.
Save pingevt/4cea26c7a2401e6fa71b5f69c55eb952 to your computer and use it in GitHub Desktop.
Generate Test Menu - Drupal 8/9
function _build_test_menu($link_title, $how_many, $full_depth = 3, $parent_id = NULL, $current_depth = 1) {
$menu_link_storage = \Drupal::entityTypeManager()->getStorage('menu_link_content');
for ($a = 1; $a <= $how_many; $a++) {
$new_title = $link_title . $a;
if ($current_depth != $full_depth) $new_title .= ".";
$item1 = $menu_link_storage->create([
'title' => $new_title,
'link' => ['uri' => 'internal:/'],
'menu_name' => 'main',
'parent' => $parent_id,
'expanded' => TRUE,
'weight' => 1,
]);
$item1->save();
if ($current_depth < $full_depth) {
_build_test_menu($new_title, $how_many, $full_depth, "menu_link_content:" . $item1->uuid(), ($current_depth + 1));
}
}
}
_build_test_menu("Item: ", 1, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment