MessageForm::form

public MessageForm::form(array $form, FormStateInterface $form_state)

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 91

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;
  }
  // Do not allow authenticated users to alter the name or email values to
  // prevent the impersonation of other users.
  else {
    $form['name']['#type'] = 'item';
    $form['name']['#value'] = $user->getDisplayName();
    $form['name']['#required'] = FALSE;
    $form['name']['#plain_text'] = $user->getDisplayName();

    $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;
}
doc_Drupal
2016-10-29 09:28:13
Comments
Leave a Comment

Please login to continue.