You are here

public function MessageForm::form in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/contact/src/MessageForm.php \Drupal\contact\MessageForm::form()

Gets the actual form array to be built.

Overrides ContentEntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

core/modules/contact/src/MessageForm.php, line 96
Contains \Drupal\contact\MessageForm.

Class

MessageForm
Form controller for contact message forms.

Namespace

Drupal\contact

Code

public function form(array $form, FormStateInterface $form_state) {
  $user = $this
    ->currentUser();
  $message = $this->entity;
  $form = parent::form($form, $form_state, $message);
  $form['#attributes']['class'][] = 'contact-form';
  if (!empty($message->preview)) {
    $form['preview'] = array(
      '#theme_wrappers' => array(
        'container__preview',
      ),
      '#attributes' => array(
        'class' => array(
          'preview',
        ),
      ),
    );
    $form['preview']['message'] = $this->entityManager
      ->getViewBuilder('contact_message')
      ->view($message, 'full');
  }
  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => $this
      ->t('Your name'),
    '#maxlength' => 255,
    '#required' => TRUE,
  );
  $form['mail'] = array(
    '#type' => 'email',
    '#title' => $this
      ->t('Your email address'),
    '#required' => TRUE,
  );
  if ($user
    ->isAnonymous()) {
    $form['#attached']['library'][] = 'core/drupal.form';
    $form['#attributes']['data-user-info-from-browser'] = TRUE;
  }
  else {
    $form['name']['#type'] = 'item';
    $form['name']['#value'] = $user
      ->getUsername();
    $form['name']['#required'] = FALSE;
    $form['name']['#plain_text'] = $user
      ->getUsername();
    $form['mail']['#type'] = 'item';
    $form['mail']['#value'] = $user
      ->getEmail();
    $form['mail']['#required'] = FALSE;
    $form['mail']['#plain_text'] = $user
      ->getEmail();
  }

  // The user contact form has a preset recipient.
  if ($message
    ->isPersonal()) {
    $form['recipient'] = array(
      '#type' => 'item',
      '#title' => $this
        ->t('To'),
      '#value' => $message
        ->getPersonalRecipient()
        ->id(),
      'name' => array(
        '#theme' => 'username',
        '#account' => $message
          ->getPersonalRecipient(),
      ),
    );
  }
  $form['copy'] = array(
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Send yourself a copy'),
    // Do not allow anonymous users to send themselves a copy, because it can
    // be abused to spam people.
    '#access' => $user
      ->isAuthenticated(),
  );
  return $form;
}