Programmatically create tokenized menu items

Created on 7 February 2019, almost 6 years ago
Updated 30 May 2023, over 1 year ago

Hi there!
Can someone please show me how to programmatically create menu items, that are already `tokenized`.
Here is a small code snippet of what I got, there is definitely something missing.

use Drupal\menu_link_content\Entity\MenuLinkContent;

/**
 * Implements hook_install().
 */
function homepages_install() {
  $items = array(
    'articles' => 'Articles',
    'brands' => 'Brands',
    'products' => 'Products'
  );

  foreach($items as $path => $title) {
    $menu_link = MenuLinkContent::create([
      'title' => $title,
      'link' => [
        'uri' => 'internal:/[current-page:url:relative]/' . $path,
        'attributes' => ['options' => ['id' => $path]],
      ],
      'menu_name' => 'category-menu',
      'menu_token_enabled' => TRUE,
      'rediscover' => FALSE,
    ]);
    $menu_link->save();
  }
}
💬 Support request
Status

Closed: duplicate

Version

9.1

Component

Code

Created by

🇧🇪Belgium AlexeiK

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024