You are here

public function PrivateMessageForm::buildForm in Private Message 8

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

Form constructor.

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

array The form structure.

Overrides EntityForm::buildForm

File

src/Form/PrivateMessageForm.php, line 179

Class

PrivateMessageForm
Defines the private message form.

Namespace

Drupal\private_message\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, PrivateMessageThreadInterface $private_message_thread = NULL) {
  $form = parent::buildForm($form, $form_state);
  if ($private_message_thread) {
    $form_state
      ->set('thread_members', $private_message_thread
      ->getMembers());
    $form['actions']['submit']['#ajax'] = [
      'callback' => '::ajaxCallback',
    ];

    // Only to do these when using #ajax.
    $form['#attached']['library'][] = 'private_message/message_form';
    $form['message']['widget'][0]['#attributes']['autofocus'] = 'autofocus';
  }
  else {

    // Create a dummy private message thread form so as to retrieve the
    // members element from it.
    $private_message_thread = PrivateMessageThread::create();
    $form_copy = $form;
    $form_state_copy = clone $form_state;
    $form_display = EntityFormDisplay::collectRenderDisplay($private_message_thread, 'default');
    $form_display
      ->buildForm($private_message_thread, $form_copy, $form_state_copy);
    $form['members'] = $form_copy['members'];
    $form['#validate'][] = '::validateMembers';
  }
  if ($this->config
    ->get('hide_form_filter_tips')) {
    $form['#after_build'][] = '::afterBuild';
  }
  return $form;
}