You are here

function groupmenu_group_insert in Group Menu 8

Implements hook_ENTITY_TYPE_insert().

File

./groupmenu.module, line 275
Gives the ability to create and manage menus for groups.

Code

function groupmenu_group_insert(EntityInterface $entity) {
  if (!$entity
    ->getGroupType()
    ->hasContentPlugin('group_menu:menu')) {
    return;
  }
  $group_type_configuration = $entity
    ->getGroupType()
    ->getContentPlugin('group_menu:menu')
    ->getConfiguration();
  $auto_create_group_menu = $group_type_configuration['auto_create_group_menu'] ?? FALSE;
  if (!$auto_create_group_menu) {
    return;
  }
  $group_menu = \Drupal::entityTypeManager()
    ->getStorage('menu')
    ->create([
    'id' => 'group-' . $entity
      ->id() . '-menu',
    'label' => $entity
      ->label() . ' menu',
    'description' => 'Menu for ' . $entity
      ->label(),
  ]);
  $group_menu
    ->save();

  // Add menu link for group if enabled.
  $auto_create_home_link = $group_type_configuration['auto_create_home_link'] ?? FALSE;
  if ($auto_create_home_link) {
    \Drupal::entityTypeManager()
      ->getStorage('menu_link_content')
      ->create([
      'title' => $group_type_configuration['auto_create_home_link_title'] ?? t('Home'),
      'link' => [
        'uri' => 'internal:/group/' . $entity
          ->id(),
      ],
      'menu_name' => $group_menu
        ->id(),
      'expanded' => TRUE,
    ])
      ->save();
  }
  $entity
    ->addContent($group_menu, 'group_menu:menu');
}