public function MessageForm::form in Drupal 8
Same name and namespace in other branches
- 9 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 101
Class
- MessageForm
- Form controller for contact message forms.
Namespace
Drupal\contactCode
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'] = [
'#theme_wrappers' => [
'container__preview',
],
'#attributes' => [
'class' => [
'preview',
],
],
];
$form['preview']['message'] = $this->entityTypeManager
->getViewBuilder('contact_message')
->view($message, 'full');
}
$form['name'] = [
'#type' => 'textfield',
'#title' => $this
->t('Your name'),
'#maxlength' => 255,
'#required' => TRUE,
];
$form['mail'] = [
'#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
->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'] = [
'#type' => 'item',
'#title' => $this
->t('To'),
'#value' => $message
->getPersonalRecipient()
->id(),
'name' => [
'#theme' => 'username',
'#account' => $message
->getPersonalRecipient(),
],
];
}
$form['copy'] = [
'#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;
}