You are here

public function ForwardForm::buildForm in Forward 4.0.x

Same name and namespace in other branches
  1. 8.3 src/Form/ForwardForm.php \Drupal\forward\Form\ForwardForm::buildForm()
  2. 8 src/Form/ForwardForm.php \Drupal\forward\Form\ForwardForm::buildForm()
  3. 8.2 src/Form/ForwardForm.php \Drupal\forward\Form\ForwardForm::buildForm()
  4. 4.x src/Form/ForwardForm.php \Drupal\forward\Form\ForwardForm::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 FormInterface::buildForm

File

src/Form/ForwardForm.php, line 269

Class

ForwardForm
Forward a page to a friend.

Namespace

Drupal\forward\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $details = FALSE) {
  $this->details = $details;
  $form_state
    ->set('#entity', $this->entity);
  $settings = $this->settings;
  $token = $this
    ->getToken($form_state);
  $langcode = $this->entity
    ->language()
    ->getId();

  // Build the form.
  $form['#title'] = $this->tokenService
    ->replace($settings['forward_form_title'], $token, [
    'langcode' => $langcode,
  ]);
  if ($details) {

    // Inline form inside a details element.
    $form['message'] = [
      '#type' => 'details',
      '#title' => $this->tokenService
        ->replace($settings['forward_form_title'], $token, [
        'langcode' => $langcode,
      ]),
      '#description' => '',
      '#open' => FALSE,
    ];
  }
  else {

    // Set the page title dynamically.
    $form['#title'] = $this->tokenService
      ->replace($settings['forward_form_title'], $token, [
      'langcode' => $langcode,
    ]);
  }
  $form['message']['instructions'] = [
    '#markup' => $this->tokenService
      ->replace($settings['forward_form_instructions'], $token, [
      'langcode' => $langcode,
    ]),
  ];
  $form['message']['email'] = [
    '#type' => 'email',
    '#title' => $this
      ->t('Your email address'),
    '#maxlength' => 254,
    '#required' => TRUE,
  ];
  $form['message']['name'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Your name'),
    '#maxlength' => 128,
    '#required' => TRUE,
  ];
  if ($settings['forward_max_recipients'] > 1) {
    $form['message']['recipient'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Send to email address'),
      '#default_value' => '',
      '#cols' => 50,
      '#rows' => 2,
      '#description' => $this
        ->t('Enter multiple addresses on separate lines or separate them with commas.'),
      '#required' => TRUE,
    ];
  }
  else {
    $form['message']['recipient'] = [
      '#type' => 'email',
      '#title' => $this
        ->t('Send to'),
      '#maxlength' => 254,
      '#description' => $this
        ->t('Enter the email address of the recipient.'),
      '#required' => TRUE,
    ];
  }
  if ($settings['forward_form_display_page']) {
    $form['message']['page'] = [
      '#type' => 'item',
      '#title' => $this
        ->t('You are going to email the following:'),
      '#markup' => $this->linkGenerator
        ->generate($this->entity
        ->label(), $this->entity
        ->toUrl()),
    ];
  }
  if ($settings['forward_form_display_subject']) {
    $form['message']['subject'] = [
      '#type' => 'item',
      '#title' => $this
        ->t('The message subject will be:'),
      '#markup' => $this->tokenService
        ->replace($settings['forward_email_subject'], $token, [
        'langcode' => $langcode,
      ]),
    ];
  }
  if ($settings['forward_form_display_body']) {
    $form['message']['body'] = [
      '#type' => 'item',
      '#title' => $this
        ->t('The introductory message text will be:'),
      '#markup' => $this->tokenService
        ->replace($settings['forward_email_message'], $token, [
        'langcode' => $langcode,
      ]),
    ];
  }
  if ($settings['forward_personal_message']) {
    $form['message']['message'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Your personal message'),
      '#default_value' => '',
      '#cols' => 50,
      '#rows' => 5,
      '#description' => $settings['forward_personal_message_filter'] ? $this
        ->t('These HTML tags are allowed in this field: @tags.', [
        '@tags' => $settings['forward_personal_message_tags'],
      ]) : $this
        ->t('HTML is not allowed in this field.'),
      '#required' => $settings['forward_personal_message'] == 2,
    ];
  }
  if ($settings['forward_form_allow_plain_text']) {
    $form['message']['email_format'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('E-mail format'),
      '#default_value' => 'html',
      '#options' => [
        'html' => $this
          ->t('HTML'),
        'plain_text' => $this
          ->t('Plain text'),
      ],
      '#required' => TRUE,
    ];
  }
  if ($details) {

    // When using details, place submit button inside it.
    $form['message']['actions'] = [
      '#type' => 'actions',
    ];
    $form['message']['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Send Message'),
      '#weight' => 100,
    ];
  }
  else {

    // When using a separate form page, use actions directly.
    $form['actions'] = [
      '#type' => 'actions',
    ];
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Send Message'),
    ];
  }

  // Default name and email address to logged in user.
  if ($this
    ->currentUser()
    ->isAuthenticated()) {
    if ($this
      ->currentUser()
      ->hasPermission('override email address')) {
      $form['message']['email']['#default_value'] = $this
        ->currentUser()
        ->getEmail();
    }
    else {

      // User not allowed to change sender email address.
      $form['message']['email']['#type'] = 'hidden';
      $form['message']['email']['#value'] = $this
        ->currentUser()
        ->getEmail();
    }
    $form['message']['name']['#default_value'] = $this
      ->currentUser()
      ->getDisplayName();
  }
  return $form;
}