You are here

PrivateMessageThreadDeleteForm.php in Private Message 8.2

Same filename and directory in other branches
  1. 8 src/Form/PrivateMessageThreadDeleteForm.php

File

src/Form/PrivateMessageThreadDeleteForm.php
View source
<?php

namespace Drupal\private_message\Form;

use Drupal\Core\Entity\ContentEntityConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\user\Entity\User;

/**
 * Form definition for the private message delete form.
 */
class PrivateMessageThreadDeleteForm extends ContentEntityConfirmFormBase {

  /**
   * {@inheritdoc}
   */
  public function getQuestion() {
    $members = $this
      ->getEntity()
      ->getMembers();
    $member_names = [];
    foreach ($members as $member) {
      if ($member
        ->id() == $this
        ->currentUser()
        ->id()) {
        continue;
      }
      $member_names[] = $member
        ->getDisplayName();
    }
    return $this
      ->t('Are you sure you want to delete the thread between you and the following users: @others?', [
      '@others' => implode(', ', $member_names),
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function getCancelUrl() {
    return Url::fromRoute('entity.private_message_thread.canonical', [
      'private_message_thread' => $this
        ->getEntity()
        ->id(),
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function getConfirmText() {
    return $this
      ->t('Delete thread');
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);
    $user = User::load($this
      ->currentUser()
      ->id());
    $this
      ->getEntity()
      ->delete($user);
    $form_state
      ->setRedirect('private_message.private_message_page');
  }

}

Classes

Namesort descending Description
PrivateMessageThreadDeleteForm Form definition for the private message delete form.