You are here

public function SubscriptionForm::buildForm in Mailing List 8

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 EntityForm::buildForm

File

src/Form/SubscriptionForm.php, line 39

Class

SubscriptionForm
Form controller for mailing list subscription form.

Namespace

Drupal\mailing_list\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  /** @var \Drupal\mailing_list\SubscriptionInterface $subscription */
  $subscription = $this->entity;

  /** @var \Drupal\mailing_list\Entity\MailingList $mailing_list */
  $mailing_list = $subscription
    ->getList();
  $form = parent::buildForm($form, $form_state);

  // Specific title for new subscription form.
  if ($subscription
    ->isNew() && $mailing_list) {
    $t_args = [
      '%name' => $mailing_list
        ->label(),
    ];
    $form['#title'] = $this
      ->currentUser()
      ->hasPermission('administer mailing list subscriptions') ? $this
      ->t('Add subscription to %name mailing list', $t_args) : $this
      ->t('Subscribe to %name mailing list', $t_args);
  }

  // Form message.
  $message_text = $this->message ?: $mailing_list
    ->getHelp();
  if (!empty($message_text) && $this->message != '<none>') {
    $form['message'] = [
      '#type' => 'html_tag',
      '#tag' => 'p',
      '#value' => $message_text,
      '#weight' => -10,
    ];
  }

  // Grouping status & authoring in tabs.
  if ($this
    ->currentUser()
    ->hasPermission('administer mailing list subscriptions')) {
    $form['advanced'] = [
      '#type' => 'vertical_tabs',
      '#weight' => 99,
    ];
    $form['subscription_authoring'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Subscription authoring'),
      '#open' => TRUE,
      '#group' => 'advanced',
    ];
    $form['uid']['#group'] = 'subscription_authoring';
    $form['created']['#group'] = 'subscription_authoring';
    $form['subscription_status'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Subscription status'),
      '#open' => TRUE,
      '#group' => 'advanced',
    ];
    $form['status']['#group'] = 'subscription_status';
  }
  return $form;
}