You are here

function og_menu_entity_insert in Organic Groups Menu (OG Menu) 8

Implements hook_entity_insert().

File

./og_menu.module, line 41
Main functions and hook implementations of the og_menu module.

Code

function og_menu_entity_insert(EntityInterface $entity) {

  // When a new group is created, automatically create a new menu for it if this
  // option is enabled.
  if (Og::isGroup($entity
    ->getEntityTypeId(), $entity
    ->bundle()) && \Drupal::config('og_menu.settings')
    ->get('autocreate')) {
    $group_content_bundle_ids = \Drupal::service('og.group_type_manager')
      ->getGroupContentBundleIdsByGroupBundle($entity
      ->getEntityTypeId(), $entity
      ->bundle());
    if (!empty($group_content_bundle_ids['ogmenu_instance'])) {
      foreach ($group_content_bundle_ids['ogmenu_instance'] as $bundle_id) {
        $values = [
          'langcode' => $entity
            ->language()
            ->getId(),
          'type' => $bundle_id,
          OgGroupAudienceHelperInterface::DEFAULT_FIELD => $entity
            ->id(),
        ];
        $og_menu_instance = OgMenuInstance::create($values);
        $og_menu_instance
          ->save();
      }
    }
  }
}