You are here

function group_content_menu_group_insert in Group Content Menu 8

Implements hook_ENTITY_TYPE_insert().

File

./group_content_menu.module, line 94
Provides a group content menu entity type.

Code

function group_content_menu_group_insert(EntityInterface $entity) {
  $plugins = group_content_menu_get_plugins_per_group($entity);

  // If there are no group menus enabled, bail.
  if (!$plugins) {
    return;
  }
  foreach ($plugins as $plugin) {
    $group_type_configuration = $plugin
      ->getConfiguration();
    $auto_create_group_menu = $group_type_configuration['auto_create_group_menu'];
    if (!$auto_create_group_menu) {
      continue;
    }
    $group_content_type = GroupContentMenuType::load($plugin
      ->getDerivativeId());
    $group_menu = \Drupal::entityTypeManager()
      ->getStorage('group_content_menu')
      ->create([
      'label' => $group_content_type
        ->label(),
      'bundle' => $plugin
        ->getDerivativeId(),
    ]);
    $group_menu
      ->save();

    // Add menu link for group if enabled.
    $auto_create_home_link = $group_type_configuration['auto_create_home_link'];
    if ($auto_create_home_link) {
      $menu_name = GroupContentMenuInterface::MENU_PREFIX . $group_menu
        ->id();
      $menu_link = \Drupal::entityTypeManager()
        ->getStorage('menu_link_content')
        ->create([
        'title' => $group_type_configuration['auto_create_home_link_title'],
        'link' => [
          'uri' => 'internal:/group/' . $entity
            ->id(),
        ],
        'menu_name' => $menu_name,
      ]);
      $menu_link
        ->save();
    }
    $group_content = \Drupal::entityTypeManager()
      ->getStorage('group_content')
      ->create([
      'type' => $plugin
        ->getContentTypeConfigId(),
      'gid' => $entity
        ->id(),
      'label' => $group_content_type
        ->label(),
      'entity_id' => $group_menu,
    ]);
    $group_content
      ->save();
  }
}