You are here

class MailgunAdminSettingsForm in Mailgun 8

Provides Mailgun configuration form.

Hierarchy

Expanded class hierarchy of MailgunAdminSettingsForm

1 string reference to 'MailgunAdminSettingsForm'
mailgun.routing.yml in ./mailgun.routing.yml
mailgun.routing.yml

File

src/Form/MailgunAdminSettingsForm.php, line 16

Namespace

Drupal\mailgun\Form
View source
class MailgunAdminSettingsForm extends ConfigFormBase {

  /**
   * Mailgun handler.
   *
   * @var \Drupal\mailgun\MailgunHandlerInterface
   */
  protected $mailgunHandler;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('config.factory'), $container
      ->get('mailgun.mail_handler'));
  }

  /**
   * {@inheritdoc}
   */
  public function __construct(ConfigFactoryInterface $config_factory, MailgunHandlerInterface $mailgun_handler) {
    parent::__construct($config_factory);
    $this->mailgunHandler = $mailgun_handler;
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      MailgunHandlerInterface::CONFIG_NAME,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'mailgun_admin_settings_form';
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form, $form_state);
    $entered_api_key = $form_state
      ->getValue('api_key');
    if (!empty($entered_api_key) && $this->mailgunHandler
      ->validateMailgunApiKey($entered_api_key) === FALSE) {
      $form_state
        ->setErrorByName('api_key', $this
        ->t("Couldn't connect to the Mailgun API. Please check your API settings."));
    }
  }

  /**
   * {@inheritdoc}
   */
  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);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $config_keys = [
      'working_domain',
      'api_key',
      'debug_mode',
      'test_mode',
      'tracking_opens',
      'tracking_clicks',
      'tracking_exception',
      'format_filter',
      'use_queue',
      'use_theme',
      'tagging_mailkey',
      'api_endpoint',
    ];
    $mailgun_config = $this
      ->config(MailgunHandlerInterface::CONFIG_NAME);
    foreach ($config_keys as $config_key) {
      if ($form_state
        ->hasValue($config_key)) {
        $mailgun_config
          ->set($config_key, $form_state
          ->getValue($config_key));
      }
    }
    $mailgun_config
      ->save();
    $this
      ->messenger()
      ->addMessage($this
      ->t('The configuration options have been saved.'));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MailgunAdminSettingsForm::$mailgunHandler protected property Mailgun handler.
MailgunAdminSettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
MailgunAdminSettingsForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
MailgunAdminSettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
MailgunAdminSettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
MailgunAdminSettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
MailgunAdminSettingsForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
MailgunAdminSettingsForm::__construct public function Constructs a \Drupal\system\ConfigFormBase object. Overrides ConfigFormBase::__construct
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.