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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | 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 ; } |
Please login to continue.