Use the microsite language when creating the menu

Created on 28 August 2024, 5 months ago
Updated 29 August 2024, 5 months ago

Problem/Motivation

If you change the micro site's language while creating it, the menu that will be created will be in a different language than the micro site's current language.

Steps to reproduce

  1. Install the "Micro Site" and "Micro Menu" modules.
  2. Create a new site type with the "Menu" option enabled.
  3. Create a new site with the previously created type and change the default language.
  4. Check if the language in the micro site's menu is different from the micro site's language.

Proposed resolution

When creating the micro site menu, set langcode to micro site's language.

/**
 * Implements hook_ENTITY_TYPE_insert().
 */
function micro_menu_site_insert(EntityInterface $entity) {
  ...
  $site_menu = $entity->getSiteMenu();
  if (empty($site_menu)) {
    /** @var \Drupal\system\Entity\Menu $menu */
    $menu = Menu::create([
      'id' => 'site-' . $entity->id(),
      'label' => 'Menu ' . $entity->label(),
      'description' => t('The menu for the site @label (id: @id)', ['@label' => $entity->label(), '@id' => $entity->id()]),
      'locked' => TRUE,
      'third_party_settings' => ['micro_menu' => ['site_id' => $entity->id()]],
      'langcode' => $entity->language()->getId(),
    ])->save();
   ...
  }
}

Remaining tasks

None.

User interface changes

None.

API changes

None.

Data model changes

None.

🐛 Bug report
Status

Needs review

Version

1.0

Component

Code

Created by

🇵🇹Portugal lolgm

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024