You are here

public function PrivateMessageForm::buildForm in Private Message 8.2

Same name and namespace in other branches
  1. 8 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 178

Class

PrivateMessageForm
Defines the private message form.

Namespace

Drupal\private_message\Form

Code

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

    // Only to do these when using #ajax.
    $form['#attached']['library'][] = 'private_message/message_form';
    $form['#attached']['drupalSettings']['privateMessageSendKey'] = $this->configFactory
      ->get('private_message.settings')
      ->get('keys_send');
    $autofocus_enabled = $this->configFactory
      ->get('private_message.settings')
      ->get('autofocus_enable');
    if ($autofocus_enabled) {
      $form['message']['widget'][0]['#attributes']['autofocus'] = 'autofocus';
    }
  }
  else {

    // Create a dummy private message thread form so as to retrieve the
    // members element from it.

    /** @var PrivateMessageThreadInterface $private_message_thread */
    $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);

    // Copy the build information from the dummy form_state object.
    if (empty($storage['field_storage']['#parents']['#fields']['members'])) {
      $storage = $form_state_copy
        ->getStorage();
      $copy_storage = $form_state_copy
        ->getStorage();
      $storage['field_storage']['#parents']['#fields']['members'] = $copy_storage['field_storage']['#parents']['#fields']['members'];
      $form_state
        ->setStorage($storage);
    }
    $form_state
      ->set('pmt_form_display', $form_display);
    $form_state
      ->set('pmt_entity', $private_message_thread);
    $form['members'] = $form_copy['members'];
    $form['#validate'][] = '::validateMembers';
  }
  if ($this->configFactory
    ->get('private_message.settings')
    ->get('hide_form_filter_tips')) {
    $form['#after_build'][] = '::afterBuild';
  }
  if ($save_label = $this->configFactory
    ->get('private_message.settings')
    ->get('save_message_label')) {
    $form['actions']['submit']['#value'] = $save_label;
  }
  return $form;
}