You are here

public function MailgunAdminSettingsForm::buildForm in Mailgun 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 ConfigFormBase::buildForm

File

src/Form/MailgunAdminSettingsForm.php, line 74

Class

MailgunAdminSettingsForm
Provides Mailgun configuration form.

Namespace

Drupal\mailgun\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $this->mailgunHandler
    ->validateMailgunLibrary(TRUE);
  $config = $this
    ->config(MailgunHandlerInterface::CONFIG_NAME);
  $form['description'] = [
    '#markup' => $this
      ->t('Please refer to @link for your settings.', [
      '@link' => Link::fromTextAndUrl($this
        ->t('dashboard'), Url::fromUri('https://app.mailgun.com/app/dashboard', [
        'attributes' => [
          'onclick' => "target='_blank'",
        ],
      ]))
        ->toString(),
    ]),
  ];
  $form['api_key'] = [
    '#title' => $this
      ->t('Mailgun API Key'),
    '#type' => 'textfield',
    '#required' => TRUE,
    '#description' => $this
      ->t('Enter your API key. It should be similar to: @key', [
      '@key' => 'key-1234567890abcdefghijklmnopqrstu',
    ]),
    '#default_value' => $config
      ->get('api_key'),
  ];

  // Load not-editable configuration object to check actual api key value
  // including overrides.
  $not_editable_config = $this
    ->configFactory()
    ->get(MailgunHandlerInterface::CONFIG_NAME);

  // Don't show other settings until we don't set API key.
  if (empty($not_editable_config
    ->get('api_key'))) {
    return parent::buildForm($form, $form_state);
  }

  // If "API Key" is overridden in settings.php it won't be visible in form.
  // We have to make the field optional and allow configure other settings.
  if (empty($config
    ->get('api_key')) && !empty($not_editable_config
    ->get('api_key'))) {
    $form['api_key']['#required'] = FALSE;
  }
  $form['working_domain'] = [
    '#title' => $this
      ->t('Mailgun API Working Domain'),
    '#type' => 'select',
    '#options' => [
      '_sender' => $this
        ->t('Get domain from sender address'),
    ] + $this->mailgunHandler
      ->getDomains(),
    '#default_value' => $config
      ->get('working_domain'),
  ];
  $form['api_endpoint'] = [
    '#title' => $this
      ->t('Mailgun Region'),
    '#type' => 'select',
    '#required' => TRUE,
    '#description' => $this
      ->t('Select which Mailgun region to use.'),
    '#options' => [
      'https://api.mailgun.net' => $this
        ->t('Default (US)'),
      'https://api.eu.mailgun.net' => $this
        ->t('Europe'),
    ],
    '#default_value' => $config
      ->get('api_endpoint'),
  ];
  $form['debug_mode'] = [
    '#title' => $this
      ->t('Enable Debug Mode'),
    '#type' => 'checkbox',
    '#default_value' => $config
      ->get('debug_mode'),
    '#description' => $this
      ->t('Enable to log every email and queuing.'),
  ];
  $form['test_mode'] = [
    '#title' => $this
      ->t('Enable Test Mode'),
    '#type' => 'checkbox',
    '#default_value' => $config
      ->get('test_mode'),
    '#description' => $this
      ->t('Mailgun will accept the message but will not send it. This is useful for testing purposes.'),
  ];
  $form['advanced_settings'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Advanced settings'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  ];
  $form['advanced_settings']['tracking'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Tracking'),
  ];
  $form['advanced_settings']['tracking']['tracking_opens'] = [
    '#title' => $this
      ->t('Enable Track Opens'),
    '#type' => 'select',
    '#options' => [
      '' => $this
        ->t('Use domain setting'),
      'no' => $this
        ->t('No'),
      'yes' => $this
        ->t('Yes'),
    ],
    '#default_value' => $config
      ->get('tracking_opens'),
    '#description' => $this
      ->t('Enable to track the opening of an email. See: @link for details.', [
      '@link' => Link::fromTextAndUrl($this
        ->t('Tracking Opens'), Url::fromUri('https://documentation.mailgun.com/en/latest/user_manual.html#tracking-opens', [
        'attributes' => [
          'onclick' => "target='_blank'",
        ],
      ]))
        ->toString(),
    ]),
  ];
  $form['advanced_settings']['tracking']['tracking_clicks'] = [
    '#title' => $this
      ->t('Enable Track Clicks'),
    '#type' => 'select',
    '#options' => [
      '' => $this
        ->t('Use domain setting'),
      'no' => $this
        ->t('No'),
      'yes' => $this
        ->t('Yes'),
      'htmlonly' => $this
        ->t('HTML only'),
    ],
    '#default_value' => $config
      ->get('tracking_clicks'),
    '#description' => $this
      ->t('Enable to track the clicks of within an email. See: @link for details.', [
      '@link' => Link::fromTextAndUrl($this
        ->t('Tracking Clicks'), Url::fromUri('https://documentation.mailgun.com/en/latest/user_manual.html#tracking-clicks', [
        'attributes' => [
          'onclick' => "target='_blank'",
        ],
      ]))
        ->toString(),
    ]),
  ];
  $form['advanced_settings']['tracking']['tracking_exception'] = [
    '#title' => $this
      ->t('Do not track the following mails'),
    '#type' => 'textarea',
    '#default_value' => $config
      ->get('tracking_exception'),
    '#description' => $this
      ->t('Add all mail keys you want to except from tracking. One key per line. Format: module:key (e.g.: user:password_reset).'),
  ];
  $form['advanced_settings']['format'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Format'),
  ];
  $options = [
    '' => $this
      ->t('None'),
  ];
  $filter_formats = filter_formats();
  foreach ($filter_formats as $filter_format_id => $filter_format) {
    $options[$filter_format_id] = $filter_format
      ->label();
  }
  $form['advanced_settings']['format']['format_filter'] = [
    '#title' => $this
      ->t('Format filter'),
    '#type' => 'select',
    '#options' => $options,
    '#default_value' => $config
      ->get('format_filter'),
    '#description' => $this
      ->t('Format filter to use to render the message.'),
  ];
  $form['advanced_settings']['format']['use_theme'] = [
    '#title' => $this
      ->t('Use theme'),
    '#type' => 'checkbox',
    '#default_value' => $config
      ->get('use_theme'),
    '#description' => $this
      ->t('Enable to pass the message through a theme function. Default "mailgun" or pass one with $message["params"]["theme"].'),
  ];
  $form['advanced_settings']['use_queue'] = [
    '#title' => $this
      ->t('Enable Queue'),
    '#type' => 'checkbox',
    '#default_value' => $config
      ->get('use_queue'),
    '#description' => $this
      ->t('Enable to queue emails and send them out during cron run. You can also enable queue for specific email keys by selecting Mailgun mailer (queued) plugin in @link.', [
      '@link' => Link::fromTextAndUrl($this
        ->t('mail system configuration'), Url::fromRoute('mailsystem.settings'))
        ->toString(),
    ]),
  ];
  $form['advanced_settings']['tagging_mailkey'] = [
    '#title' => $this
      ->t('Enable tags by mail key'),
    '#type' => 'checkbox',
    '#default_value' => $config
      ->get('tagging_mailkey'),
    '#description' => $this
      ->t('Add tag by mail key. See @link for details. Warning: adding tags will automatically add the "List-Unsubscribe" header to e-emails.', [
      '@link' => Link::fromTextAndUrl($this
        ->t("Mailgun's tagging documentation"), Url::fromUri('https://documentation.mailgun.com/en/latest/user_manual.html#tagging', [
        'attributes' => [
          'onclick' => "target='_blank'",
        ],
      ]))
        ->toString(),
    ]),
  ];
  return parent::buildForm($form, $form_state);
}