You are here

public function PrivateMessageForm::save in Private Message 8

Same name and namespace in other branches
  1. 8.2 src/Form/PrivateMessageForm.php \Drupal\private_message\Form\PrivateMessageForm::save()

Form submission handler for the 'save' action.

Normally this method should be overridden to provide specific messages to the user and redirect the form after the entity has been saved.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

int Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.

Overrides EntityForm::save

File

src/Form/PrivateMessageForm.php, line 287

Class

PrivateMessageForm
Defines the private message form.

Namespace

Drupal\private_message\Form

Code

public function save(array $form, FormStateInterface $form_state) {
  $status = parent::save($form, $form_state);
  $members = $form_state
    ->get('thread_members');
  if (!$members) {

    // Generate an array containing the members of the thread.
    $current_user = $this->userManager
      ->load($this->currentUser
      ->id());
    $members = [
      $current_user,
    ];
    foreach ($form_state
      ->getValue('members') as $info) {
      $user = $this->userManager
        ->load($info['target_id']);
      if ($user) {
        $members[] = $user;
      }
    }
  }

  // Get a private message thread containing the given users.
  $private_message_thread = $this->privateMessageService
    ->getThreadForMembers($members);

  // Save the thread.
  $this->privateMessageThreadManager
    ->saveThread($this->entity, $members, [], $private_message_thread);

  // Send the user to the private message page. As this thread is the newest,
  // it wll be at the top of the list.
  $form_state
    ->setRedirect('entity.private_message_thread.canonical', [
    'private_message_thread' => $private_message_thread
      ->id(),
  ]);
  return $status;
}