You are here

public function MailingListForm::form in Mailing List 8

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/Form/MailingListForm.php, line 45

Class

MailingListForm
Form handler for mailing list forms.

Namespace

Drupal\mailing_list\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);

  /** @var \Drupal\mailing_list\MailingListInterface $mailing_list */
  $mailing_list = $this->entity;
  $form['#title'] = $this->operation == 'add' ? $this
    ->t('Add a new mailing list') : $this
    ->t('Edit %label mailing list', [
    '%label' => $mailing_list
      ->label(),
  ]);
  $form['label'] = [
    '#title' => $this
      ->t('Name'),
    '#type' => 'textfield',
    '#default_value' => $mailing_list
      ->label(),
    '#description' => $this
      ->t('The human-readable name of this mailing list. This name must be unique.'),
    '#required' => TRUE,
    '#size' => 30,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $mailing_list
      ->id(),
    '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
    '#machine_name' => [
      'exists' => [
        'Drupal\\mailing_list\\Entity\\MailingList',
        'load',
      ],
      'source' => [
        'label',
      ],
    ],
    '#description' => $this
      ->t('A unique machine-readable name for this mailing list. It must only contain lowercase letters, numbers, and underscores.'),
  ];
  $form['description'] = [
    '#title' => $this
      ->t('Description'),
    '#type' => 'textarea',
    '#rows' => 2,
    '#default_value' => $mailing_list
      ->getDescription(),
    '#description' => $this
      ->t('This text will be displayed on subscriptions management pages for this mailing list.'),
  ];
  $form['help'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Explanation or submission guidelines'),
    '#rows' => 2,
    '#default_value' => $mailing_list
      ->getHelp(),
    '#description' => $this
      ->t('Help information shown to the user when creating a new subscription on this list.'),
  ];
  $form['additional_settings'] = [
    '#type' => 'vertical_tabs',
  ];
  $form['control'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Subscription control'),
    '#open' => TRUE,
    '#group' => 'additional_settings',
  ];
  $form['control']['cross_access'] = [
    '#title' => $this
      ->t('Allow subscription cross access'),
    '#description' => $this
      ->t('Grant access to non-owned subscriptions through the subscription management hashed URL.'),
    '#type' => 'checkbox',
    '#default_value' => $mailing_list
      ->isCrossAccessAllowed(),
  ];
  $form['control']['form_destination'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Form destination'),
    '#options' => [
      '' => $this
        ->t('- Default -'),
      'canonical' => $this
        ->t('View subscription'),
      'edit-form' => $this
        ->t('Edit subscription'),
      'manage' => $this
        ->t('Manage subscriptions'),
    ],
    '#description' => $this
      ->t('Where to go after the subscription is created.'),
    '#default_value' => $mailing_list
      ->getFormDestination(),
  ];
  $form['control']['max_per_user'] = [
    '#title' => $this
      ->t('Limit per user'),
    '#description' => $this
      ->t('Max allowed subscriptions per user. Enter 0 for no limit.'),
    '#type' => 'number',
    '#default_value' => $mailing_list
      ->getLimitByUser() ?: 0,
    '#group' => 'additional_settings',
  ];
  $form['control']['max_per_email'] = [
    '#title' => $this
      ->t('Limit per email'),
    '#description' => $this
      ->t('Max allowed subscriptions per email address. Enter 0 for no limit.'),
    '#type' => 'number',
    '#default_value' => $mailing_list
      ->getLimitByEmail() ?: 0,
    '#group' => 'additional_settings',
  ];
  $secs_per_week = 7 * 24 * 60 * 60;
  $options = [
    $secs_per_week => $this
      ->t('1 week'),
  ];
  for ($i = 2; $i <= 7; $i++) {
    $options[$secs_per_week * $i] = $this
      ->t('@count weeks', [
      '@count' => $i,
    ]);
  }
  for ($i = 2; $i <= 11; $i++) {
    $options[round($i * 4.33)] = $this
      ->t('@count months', [
      '@count' => $i,
    ]);
  }
  $options[$secs_per_week * 52] = $this
    ->t('1 year');
  for ($i = 2; $i <= 5; $i++) {
    $options[$secs_per_week * 52 * $i] = $this
      ->t('@count years', [
      '@count' => $i,
    ]);
  }
  $options[0] = $this
    ->t('- Do not purge -');
  $form['control']['inactive_subscriptions_liftime'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Inactive subscriptions lifetime'),
    '#description' => $this
      ->t('Inactive subscriptions older than this value will be purged from database.'),
    '#options' => $options,
    '#default_value' => $mailing_list
      ->getInactiveLifetime() ?: 0,
    '#group' => 'additional_settings',
  ];
  $form['messages'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Subscription operation messages'),
    '#group' => 'additional_settings',
  ];
  $form['messages']['subscription_message'] = [
    '#type' => 'textarea',
    '#title' => t('On subscription message'),
    '#description' => $this
      ->t('Message to the subscriber after a new subscription has done.'),
    '#rows' => 2,
    '#default_value' => $mailing_list
      ->getOnSubscriptionMessage(),
  ];
  $form['messages']['cancellation_message'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('On cancellation message'),
    '#description' => $this
      ->t('Message to the subscriber on subscription cancellation.'),
    '#rows' => 2,
    '#default_value' => $mailing_list
      ->getOnCancellationMessage(),
  ];
  return $this
    ->protectBundleIdElement($form);
}