You are here

function activity_creator_post_update_8803_remove_activities_with_no_related_entities in Open Social 10.2.x

Same name and namespace in other branches
  1. 8.9 modules/custom/activity_creator/activity_creator.post_update.php \activity_creator_post_update_8803_remove_activities_with_no_related_entities()
  2. 8.8 modules/custom/activity_creator/activity_creator.post_update.php \activity_creator_post_update_8803_remove_activities_with_no_related_entities()
  3. 10.3.x modules/custom/activity_creator/activity_creator.post_update.php \activity_creator_post_update_8803_remove_activities_with_no_related_entities()
  4. 10.0.x modules/custom/activity_creator/activity_creator.post_update.php \activity_creator_post_update_8803_remove_activities_with_no_related_entities()
  5. 10.1.x modules/custom/activity_creator/activity_creator.post_update.php \activity_creator_post_update_8803_remove_activities_with_no_related_entities()

Remove activities notification status if it's related entity not exist.

File

modules/custom/activity_creator/activity_creator.post_update.php, line 152
Contains post update hook implementations.

Code

function activity_creator_post_update_8803_remove_activities_with_no_related_entities(&$sandbox) {
  $database = \Drupal::database();
  if (!isset($sandbox['activities_id'])) {

    // Get activity ids from entity table.
    $activity_ids = $database
      ->select('activity', 'aid')
      ->fields('aid', [
      'id',
    ])
      ->execute()
      ->fetchCol();

    // Get activity ids from activity__field_activity_entity table.
    // This table contains data of field_activity_entity which tells us about
    // any related entity to an activity.
    $afce_ids = $database
      ->select('activity__field_activity_entity', 'afce')
      ->fields('afce', [
      'entity_id',
    ])
      ->execute()
      ->fetchCol();

    // 'count' is the number of total records we’ll be processing.
    $sandbox['count'] = 0;

    // We take store a diff of both the results which will contain the result
    // of activity ids which doesn't have valid referenced entities any more.
    // We will remove them in batch later. Also, we are only checking
    // $activity_id to be not empty because $afce_ids is null, that means we
    // shall remove all activities as none of them will have valid referenced
    // entity.
    if (!empty($activity_ids)) {
      $sandbox['activities_id'] = array_diff($activity_ids, $afce_ids);
      $sandbox['count'] = count($sandbox['activities_id']);
    }

    // If 'count' is empty, we have nothing to process.
    if (empty($sandbox['count'])) {
      $sandbox['#finished'] = 1;
      return;
    }

    // 'progress' will represent the current progress of our processing.
    $sandbox['progress'] = 0;

    // 'activities_per_batch' is a custom amount that we’ll use to limit
    // how many activities we’re processing in each batch.
    // The variables value can be declared in settings file of Drupal.
    $sandbox['activities_per_batch'] = Settings::get('activity_update_batch_size', 100);
  }

  // Extract activity ids for deletion.
  $aids_for_delete = array_splice($sandbox['activities_id'], 0, $sandbox['activities_per_batch']);

  // Now let’s remove the activities with no related entities.
  $storage = \Drupal::entityTypeManager()
    ->getStorage('activity');
  $activities = $storage
    ->loadMultiple($aids_for_delete);
  $storage
    ->delete($activities);

  // Remove entries from activity_notification_table.
  $activity_notification_service = \Drupal::service('activity_creator.activity_notifications');
  $activity_notification_service
    ->deleteNotificationsbyIds($aids_for_delete);

  // Calculates current batch range.
  $range_end = $sandbox['progress'] + $sandbox['activities_per_batch'];
  if ($range_end > $sandbox['count']) {
    $range_end = $sandbox['count'];
  }

  // Update the batch variables to track our progress.
  $sandbox['progress'] = $range_end;

  // We can calculate our current progress via a mathematical fraction.
  $progress_fraction = $sandbox['progress'] / $sandbox['count'];

  // Tell the Batch API about status of this process.
  $sandbox['#finished'] = empty($sandbox['activities_id']) ? 1 : ($sandbox['count'] - count($sandbox['activities_id'])) / $sandbox['count'];
}