Skip to main content
Drupal 8

Main navigation

  • Home
    • Question And Answer
    • NEWS
    • Projects
  • Tutorials - Drupal
    • Android
    • Other
User account menu
  • Log in

Breadcrumb

  1. Home

Create and translate menu links in Drupal 8

By editor , 28 November, 2015
Question

How to programmatically create and translate menu links ?

Programmatically create and translate menu links.

Example:
use Drupal\menu_link_content\Entity\MenuLinkContent;
$menu_item = MenuLinkContent::create([
  'bundle' => 'menu_link_content',
  'langcode' => 'en',
  'title' => 'My menu link',
  'description' => 'My description.',
  'menu_name' => 'main',
  'link' => [['uri' => 'internal:/taxonomy/term/15']],
  'parent' => 'menu_link_content:c8859c9c-a550-45a9-a705-5d032dee525c',
  'weight' => 0,
]);
$menu_item->save();
$menu_item_es = $menu_item->addTranslation('es');
$menu_item_es->title = 'Mi enlace del menú';
$menu_item_es->description = 'Mi descripción.';
$menu_item_es->save();

Tags

  • Drupal 8
  • Code

Comments

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

Links

  • Drupal.org
  • Drupal core release cycle
  • Drupal events with ease
  • Unit test with Drupal using PHPUnit (Drupal 10)
  • Tokens for file upload url
  • Tips for Docker
  • CKEditor inline image files usage for custom forms.
  • Install Docker on Linux Mint
  • Install PHP 8.1
  • Drupal Security Best Practices
  • Order and sort items with tabledrag
  • Add a custom service YAML container
  • Apply a patch with composer