You are here

public function PrivateMessageCommands::prepareUninstall in Private Message 8.2

Prepares the Private Message module for uninstallation.

@command private_message:prepare_uninstall

@aliases pu

File

src/Commands/PrivateMessageCommands.php, line 64

Class

PrivateMessageCommands
Creates Drush commands for the Private Message module.

Namespace

Drupal\private_message\Commands

Code

public function prepareUninstall() {
  if ($this
    ->io()
    ->confirm($this
    ->t('Proceeding will delete all private messages and private message threads in your system. They cannot be recovered. Do you wish to proceed?')
    ->render())) {

    // Get the IDs of all threads in the system.
    $thread_ids = $this->privateMessageService
      ->getThreadIds();

    // Load the private message thread storage.
    $thread_storage = $this->entityTypeManager
      ->getStorage('private_message_thread');
    $message_storage = $this->entityTypeManager
      ->getStorage('private_message');
    $output = $this
      ->output();

    // Note that each thread ID is looped through, as there may be a massive
    // number of threads in the system, meaning that loading them all could be
    // a crazy memory hog and crash.
    foreach ($thread_ids as $thread_id) {

      // Load the thread.
      $thread = $thread_storage
        ->load($thread_id);
      $message_storage
        ->delete($thread
        ->getMessages());

      // Delete the thread.
      $thread_storage
        ->delete([
        $thread,
      ]);

      // Notify the user.
      $output
        ->writeln($this
        ->t('Deleted thread: @id', [
        '@id' => $thread_id,
      ])
        ->render());
    }

    // Inform the user the process is finished.
    $output
      ->writeln($this
      ->t('All private message content deleted.')
      ->render());
  }
}