You are here

class PrivateMessageCommands in Private Message 8.2

Creates Drush commands for the Private Message module.

In addition to this file, you need a drush.services.yml in root of your module, and a composer.json file that provides the name of the services file to use.

See these files for an example of injecting Drupal services:

Hierarchy

Expanded class hierarchy of PrivateMessageCommands

1 string reference to 'PrivateMessageCommands'
drush.services.yml in ./drush.services.yml
drush.services.yml
1 service uses PrivateMessageCommands
private_message.commands in ./drush.services.yml
\Drupal\private_message\Commands\PrivateMessageCommands

File

src/Commands/PrivateMessageCommands.php, line 21

Namespace

Drupal\private_message\Commands
View source
class PrivateMessageCommands extends DrushCommands {
  use StringTranslationTrait;

  /**
   * The private message service.
   *
   * @var \Drupal\private_message\Service\PrivateMessageService
   */
  protected $privateMessageService;

  /**
   * The entity type manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs a  object.
   *
   * @param \Drupal\private_message\Service\PrivateMessageService $privateMessageService
   *   The private message service.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   The entity type manager service.
   */
  public function __construct(PrivateMessageService $privateMessageService, EntityTypeManagerInterface $entityTypeManager) {
    parent::__construct();
    $this->privateMessageService = $privateMessageService;
    $this->entityTypeManager = $entityTypeManager;
  }

  /**
   * Prepares the Private Message module for uninstallation.
   *
   * @command private_message:prepare_uninstall
   *
   * @aliases pu
   */
  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());
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PrivateMessageCommands::$entityTypeManager protected property The entity type manager service.
PrivateMessageCommands::$privateMessageService protected property The private message service.
PrivateMessageCommands::prepareUninstall public function Prepares the Private Message module for uninstallation.
PrivateMessageCommands::__construct public function Constructs a object.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.