You are here

function og_entity_predelete in Organic groups 8

Implements hook_entity_predelete().

File

./og.module, line 71

Code

function og_entity_predelete(EntityInterface $entity) {
  if (Og::isGroup($entity
    ->getEntityTypeId(), $entity
    ->bundle())) {

    // Register orphaned group content and user memberships for deletion, if
    // this option has been enabled.
    $config = \Drupal::config('og.settings');
    if ($config
      ->get('delete_orphans')) {
      $plugin_id = $config
        ->get('delete_orphans_plugin_id');

      /** @var \Drupal\og\OgDeleteOrphansInterface $plugin */
      $plugin = \Drupal::service('plugin.manager.og.delete_orphans')
        ->createInstance($plugin_id, []);
      $plugin
        ->register($entity);
    }

    // @todo Delete user roles.
    // @see https://github.com/amitaibu/og/issues/175
    // og_delete_user_roles_by_group($entity_type, $entity);
  }

  // If a user is being deleted, also delete its memberships.
  if ($entity instanceof UserInterface) {

    /** @var \Drupal\og\MembershipManagerInterface $membership_manager */
    $membership_manager = \Drupal::service('og.membership_manager');
    foreach ($membership_manager
      ->getMemberships($entity
      ->id(), []) as $membership) {
      $membership
        ->delete();
    }
  }
}