You are here

public function CampaignMonitorSubscribeForm::buildForm in Campaign Monitor 8

Same name and namespace in other branches
  1. 8.2 src/Form/CampaignMonitorSubscribeForm.php \Drupal\campaignmonitor\Form\CampaignMonitorSubscribeForm::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/CampaignMonitorSubscribeForm.php, line 62

Class

CampaignMonitorSubscribeForm
Subscribe to a campaignmonitor list.

Namespace

Drupal\campaignmonitor\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $config = []) {

  // Try to get the e-mail address from the user object.
  if (\Drupal::currentUser()
    ->id() != 0) {
    $account = User::load(\Drupal::currentUser()
      ->id());
    $email = $account
      ->get('mail')
      ->getValue()[0]['value'];
  }
  $form['email'] = [
    '#type' => 'textfield',
    '#title' => t('Email'),
    '#required' => TRUE,
    '#maxlength' => 200,
    '#default_value' => isset($email) ? $email : '',
  ];
  switch ($config['list']) {
    case 'single':
      $form += $this
        ->singleSubscribeForm($form, $form_state, $config);
      $this
        ->setFormID($this->formId . '_single');
      break;
    default:
      $form += $this
        ->userSelectSubscribeForm($form, $form_state, $config);
  }
  $form['submit'] = [
    '#type' => 'submit',
    '#value' => t('Subscribe'),
  ];
  $form['config'] = [
    '#type' => 'hidden',
    '#value' => serialize($config),
  ];
  return $form;
}