You are here

public function MandrillAdminSettingsForm::buildForm in Mandrill 8

.

Overrides ConfigFormBase::buildForm

File

src/Form/MandrillAdminSettingsForm.php, line 96

Class

MandrillAdminSettingsForm
Implements an Mandrill Admin Settings form.

Namespace

Drupal\mandrill\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->config('mandrill.settings');
  $key = $config
    ->get('mandrill_api_key');
  $form['mandrill_api_key'] = array(
    '#title' => $this
      ->t('Mandrill API Key'),
    '#type' => 'textfield',
    '#description' => $this
      ->t('Create or grab your API key from the %link.', array(
      '%link' => Link::fromTextAndUrl($this
        ->t('Mandrill settings'), Url::fromUri('https://mandrillapp.com/settings/index'))
        ->toString(),
    )),
    '#default_value' => $key,
  );
  if (!$this->mandrillAPI
    ->isLibraryInstalled()) {
    $this
      ->messenger()
      ->addWarning($this
      ->t('The Mandrill PHP library is not installed. Please see installation directions in README.txt'));
  }
  else {
    if ($key) {
      $mailSystemPath = Url::fromRoute('mailsystem.settings');
      $usage = [];
      foreach ($this->mandrill
        ->getMailSystems() as $system) {
        if ($this
          ->mailConfigurationUsesMandrillMail($system)) {
          $system['sender'] = $this
            ->getPluginLabel($system['sender']);
          $system['formatter'] = $this
            ->getPluginLabel($system['formatter']);
          $usage[] = $system;
        }
      }
      if (!empty($usage)) {
        $usage_array = array(
          '#theme' => 'table',
          '#header' => array(
            $this
              ->t('Key'),
            $this
              ->t('Sender'),
            $this
              ->t('Formatter'),
          ),
          '#rows' => $usage,
        );
        $form['mandrill_status'] = array(
          '#type' => 'markup',
          '#markup' => $this
            ->t('Mandrill is currently configured to be used by the following Module Keys. To change these settings or ' . 'configure additional systems to use Mandrill, use %link.<br /><br />%table', array(
            '%link' => Link::fromTextAndUrl($this
              ->t('Mail System'), $mailSystemPath)
              ->toString(),
            '%table' => $this->renderer
              ->render($usage_array),
          )),
        );
      }
      elseif (!$form_state
        ->get('rebuild')) {
        $this
          ->messenger()
          ->addWarning($this
          ->t('PLEASE NOTE: Mandrill is not currently configured for use by Drupal. In order to route your email through Mandrill, ' . 'you must configure at least one MailSystemInterface (other than mandrill) to use "MandrillMailSystem" in %link, or ' . 'you will only be able to send Test Emails through Mandrill.', array(
          '%link' => Link::fromTextAndUrl($this
            ->t('Mail System'), $mailSystemPath)
            ->toString(),
        )));
      }
      $form['email_options'] = array(
        '#type' => 'fieldset',
        '#collapsible' => TRUE,
        '#title' => $this
          ->t('Email options'),
      );
      $form['email_options']['mandrill_from'] = array(
        '#title' => $this
          ->t('From address'),
        '#type' => 'textfield',
        '#description' => $this
          ->t('The sender email address. If this address has not been verified, messages will be queued and not sent until it is. ' . 'This address will appear in the "from" field, and any emails sent through Mandrill with a "from" address will have that ' . 'address moved to the Reply-To field.'),
        '#default_value' => $config
          ->get('mandrill_from_email'),
      );
      $form['email_options']['mandrill_from_name'] = array(
        '#type' => 'textfield',
        '#title' => $this
          ->t('From name'),
        '#default_value' => $config
          ->get('mandrill_from_name'),
        '#description' => $this
          ->t('Optionally enter a from name to be used.'),
      );
      $sub_accounts = $this->mandrillAPI
        ->getSubAccounts();
      $sub_accounts_options = array();
      if (!empty($sub_accounts)) {
        $sub_accounts_options = array(
          '_none' => '-- Select --',
        );
        foreach ($sub_accounts as $account) {
          if ($account['status'] == 'active') {
            $sub_accounts_options[$account['id']] = $account['name'] . ' (' . $account['reputation'] . ')';
          }
        }
      }
      elseif ($config
        ->get('mandrill_subaccount')) {
        $config
          ->set('mandrill_subaccount', FALSE)
          ->save();
      }
      if (!empty($sub_accounts_options)) {
        $form['email_options']['mandrill_subaccount'] = array(
          '#type' => 'select',
          '#title' => $this
            ->t('Subaccount'),
          '#options' => $sub_accounts_options,
          '#default_value' => $config
            ->get('mandrill_subaccount'),
          '#description' => $this
            ->t('Choose a subaccount to send through.'),
        );
      }
      $formats = filter_formats();
      $options = array(
        '' => $this
          ->t('-- Select --'),
      );
      foreach ($formats as $v => $format) {
        $options[$v] = $format
          ->get('name');
      }
      $form['email_options']['mandrill_filter_format'] = array(
        '#type' => 'select',
        '#title' => $this
          ->t('Input format'),
        '#description' => $this
          ->t('If selected, the input format to apply to the message body before sending to the Mandrill API.'),
        '#options' => $options,
        '#default_value' => array(
          $config
            ->get('mandrill_filter_format'),
        ),
      );
      $form['send_options'] = array(
        '#title' => $this
          ->t('Send options'),
        '#type' => 'fieldset',
        '#collapsible' => TRUE,
      );
      $form['send_options']['mandrill_track_opens'] = array(
        '#title' => $this
          ->t('Track opens'),
        '#type' => 'checkbox',
        '#description' => $this
          ->t('Whether or not to turn on open tracking for messages.'),
        '#default_value' => $config
          ->get('mandrill_track_opens'),
      );
      $form['send_options']['mandrill_track_clicks'] = array(
        '#title' => $this
          ->t('Track clicks'),
        '#type' => 'checkbox',
        '#description' => $this
          ->t('Whether or not to turn on click tracking for messages.'),
        '#default_value' => $config
          ->get('mandrill_track_clicks'),
      );
      $form['send_options']['mandrill_url_strip_qs'] = array(
        '#title' => $this
          ->t('Strip query string'),
        '#type' => 'checkbox',
        '#description' => $this
          ->t('Whether or not to strip the query string from URLs when aggregating tracked URL data.'),
        '#default_value' => $config
          ->get('mandrill_url_strip_qs'),
      );
      $form['send_options']['mandrill_mail_key_blacklist'] = array(
        '#title' => $this
          ->t('Content logging blacklist'),
        '#type' => 'textarea',
        '#description' => $this
          ->t('Comma delimited list of Drupal mail keys to exclude content logging for. CAUTION: Removing the default password reset key may expose a security risk.'),
        '#default_value' => $config
          ->get('mandrill_mail_key_blacklist'),
      );
      $form['send_options']['mandrill_log_defaulted_sends'] = array(
        '#title' => $this
          ->t('Log sends from module/key pairs that are not registered independently in mailsystem.'),
        '#type' => 'checkbox',
        '#description' => $this
          ->t('If you select Mandrill as the site-wide default email sender in %mailsystem and check this box, any messages that are sent through Mandrill using module/key pairs that are not specifically registered in mailsystem will cause a message to be written to the system log (type: Mandrill, severity: info). Enable this to identify keys and modules for automated emails for which you would like to have more granular control. It is not recommended to leave this box checked for extended periods, as it slows Mandrill and can clog your logs.', array(
          '%mailsystem' => Link::fromTextAndUrl($this
            ->t('Mail System'), $mailSystemPath)
            ->toString(),
        )),
        '#default_value' => $config
          ->get('mandrill_log_defaulted_sends'),
      );
      $form['analytics'] = array(
        '#type' => 'fieldset',
        '#collapsible' => TRUE,
        '#title' => $this
          ->t('Google analytics'),
      );
      $form['analytics']['mandrill_analytics_domains'] = array(
        '#title' => $this
          ->t('Google analytics domains'),
        '#type' => 'textfield',
        '#description' => $this
          ->t('One or more domains for which any matching URLs will automatically have Google Analytics parameters appended to their query string. Separate each domain with a comma.'),
        '#default_value' => $config
          ->get('mandrill_analytics_domains'),
      );
      $form['analytics']['mandrill_analytics_campaign'] = array(
        '#title' => $this
          ->t('Google analytics campaign'),
        '#type' => 'textfield',
        '#description' => $this
          ->t("The value to set for the utm_campaign tracking parameter. If this isn't provided the messages from address will be used instead."),
        '#default_value' => $config
          ->get('mandrill_analytics_campaign'),
      );
      $form['asynchronous_options'] = array(
        '#title' => $this
          ->t('Asynchronous options'),
        '#type' => 'fieldset',
        '#collapsible' => TRUE,
        '#attributes' => array(
          'id' => array(
            'mandrill-async-options',
          ),
        ),
      );
      $form['asynchronous_options']['mandrill_process_async'] = array(
        '#title' => $this
          ->t('Queue outgoing messages'),
        '#type' => 'checkbox',
        '#description' => $this
          ->t('When set, emails will not be immediately sent. Instead, they will be placed in a queue and sent when cron is triggered.'),
        '#default_value' => $config
          ->get('mandrill_process_async'),
      );
      $form['asynchronous_options']['mandrill_batch_log_queued'] = array(
        '#title' => $this
          ->t('Log queued emails'),
        '#type' => 'checkbox',
        '#description' => $this
          ->t('Do you want to create a log entry when an email is queued to be sent?'),
        '#default_value' => $config
          ->get('mandrill_batch_log_queued'),
        '#states' => array(
          'invisible' => array(
            ':input[name="mandrill_process_async"]' => array(
              'checked' => FALSE,
            ),
          ),
        ),
      );
      $form['asynchronous_options']['mandrill_queue_worker_timeout'] = array(
        '#title' => $this
          ->t('Queue worker timeout'),
        '#type' => 'textfield',
        '#size' => '12',
        '#description' => $this
          ->t('Number of seconds to spend processing messages during cron. Zero or negative values are not allowed.'),
        //'#required' => TRUE,

        //'#element_validate' => array('element_validate_integer_positive'),
        '#default_value' => $config
          ->get('mandrill_queue_worker_timeout'),
        '#states' => array(
          'invisible' => array(
            ':input[name="mandrill_process_async"]' => array(
              'checked' => FALSE,
            ),
          ),
        ),
      );
    }
  }
  $form['actions']['#type'] = 'actions';
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => $this
      ->t('Save'),
    '#button_type' => 'primary',
  );
  return parent::buildForm($form, $form_state);
}