You are here

public function ForwardForm::buildForm in Forward 8

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

Class

ForwardForm
Forward a page to a friend

Namespace

Drupal\forward\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, array $settings = NULL) {
  if (!$settings) {
    $settings = $this
      ->config('forward.settings')
      ->get();
  }
  $this->settings = $settings;
  $form_state
    ->set('#entity', $this->entity);
  $token = $this
    ->getToken($form_state);
  $langcode = $this->entity
    ->language()
    ->getId();

  // Build the form
  if ($settings['forward_interface_type'] == 'link') {

    // Set the page title dynamically
    $form['#title'] = $this->tokenService
      ->replace($settings['forward_link_title'], $token, array(
      'langcode' => $langcode,
    ));
  }
  else {

    // Inline form
    $form['message'] = array(
      '#type' => 'details',
      '#title' => $this->tokenService
        ->replace($settings['forward_link_title'], $token, array(
        'langcode' => $langcode,
      )),
      '#description' => '',
      '#open' => FALSE,
      '#weight' => $settings['forward_interface_weight'],
    );
  }
  $form['message']['instructions'] = array(
    '#markup' => $this->tokenService
      ->replace($settings['forward_form_instructions'], $token, array(
      'langcode' => $langcode,
    )),
  );
  $form['message']['email'] = array(
    '#type' => 'email',
    '#title' => $this
      ->t('Your email address'),
    '#maxlength' => 254,
    '#required' => TRUE,
  );
  $form['message']['name'] = array(
    '#type' => 'textfield',
    '#title' => $this
      ->t('Your name'),
    '#maxlength' => 128,
    '#required' => TRUE,
  );
  $form['message']['recipient'] = array(
    '#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'] = array(
      '#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'] = array(
      '#type' => 'item',
      '#title' => $this
        ->t('The message subject will be:'),
      '#markup' => $this->tokenService
        ->replace($settings['forward_email_subject'], $token, array(
        'langcode' => $langcode,
      )),
    );
  }
  if ($settings['forward_form_display_body']) {
    $form['message']['body'] = array(
      '#type' => 'item',
      '#title' => $this
        ->t('The introductory message text will be:'),
      '#markup' => $this->tokenService
        ->replace($settings['forward_email_message'], $token, array(
        'langcode' => $langcode,
      )),
    );
  }
  if ($settings['forward_personal_message']) {
    $form['message']['message'] = array(
      '#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.', array(
        '@tags' => $settings['forward_personal_message_tags'],
      )) : t('HTML is not allowed in this field.'),
      '#required' => $settings['forward_personal_message'] == 2,
    );
  }

  // Submit button
  if ($settings['forward_interface_type'] == 'form') {

    // When using a collapsible form, move submit button into fieldset
    $form['message']['actions'] = array(
      '#type' => 'actions',
    );
    $form['message']['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => $this
        ->t('Send Message'),
      '#weight' => 100,
    );
  }
  else {

    // When using a separate form page, use actions directly
    $form['actions'] = array(
      '#type' => 'actions',
    );
    $form['actions']['submit'] = array(
      '#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;
}