You are here

class PostmarkSettingsForm in Postmark 8

Configure Postmark settings.

Hierarchy

Expanded class hierarchy of PostmarkSettingsForm

1 string reference to 'PostmarkSettingsForm'
postmark.routing.yml in ./postmark.routing.yml
postmark.routing.yml

File

src/Form/PostmarkSettingsForm.php, line 14

Namespace

Drupal\postmark\Form
View source
class PostmarkSettingsForm extends ConfigFormBase {

  /**
   * The core mail manager service.
   *
   * @var \Drupal\postmark\PostmarkHandler
   */
  protected $postmarkHandler;

  /**
   * Constructs a Postmark settings form.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   * @param \Drupal\postmark\PostmarkHandler $postmark_handler
   *   The core mail manager service.
   */
  public function __construct(ConfigFactoryInterface $config_factory, PostmarkHandler $postmark_handler) {
    parent::__construct($config_factory);
    $this->postmarkHandler = $postmark_handler;
  }

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

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

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'postmark.settings',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {

    // The user's Postmark API key
    $form['postmark_api_key'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Postmark API Token'),
      '#default_value' => $this
        ->config('postmark.settings')
        ->get('postmark_api_key'),
      '#description' => $this
        ->t('The Server API token similar to ed742D75-5a45-49b6-a0a1-5b9ec3dc9e5d, generated on the Postmark Server Credentials page.'),
      '#required' => TRUE,
    ];
    $form['postmark_sender_signature'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Postmark Sender Signature'),
      '#default_value' => $this
        ->config('postmark.settings')
        ->get('postmark_sender_signature'),
      '#description' => $this
        ->t('The email address configured within Postmark as the Sender Signature for the server associated with the Server API token.'),
      '#required' => TRUE,
    ];
    $form['debug'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Debugging'),
      '#open' => FALSE,
    ];
    $form['debug']['postmark_debug_mode'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable Postmark debugging'),
      '#default_value' => $this
        ->config('postmark.settings')
        ->get('postmark_debug_mode'),
    ];
    $form['debug']['postmark_debug_email'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Enable Postmark debugging email'),
      '#default_value' => $this
        ->config('postmark.settings')
        ->get('postmark_debug_email'),
      '#description' => $this
        ->t('Use a debugging email, so all system emails will go to this address. Debugging mode must be on for this to work'),
    ];
    $form['debug']['postmark_debug_no_send'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable Postmark debugging to not send an email and therefore not use a credit'),
      '#default_value' => $this
        ->config('postmark.settings')
        ->get('postmark_debug_no_send'),
    ];
    $form['test'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Test email'),
      '#open' => FALSE,
    ];
    $form['test']['test_address'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Recipient'),
      '#default_value' => '',
      '#description' => $this
        ->t('Enter a valid email address to send a test email.'),
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $config = $this
      ->config('postmark.settings');
    $config
      ->set('postmark_api_key', $form_state
      ->getValue('postmark_api_key'))
      ->set('postmark_sender_signature', $form_state
      ->getValue('postmark_sender_signature'))
      ->set('postmark_debug_mode', $form_state
      ->getValue('postmark_debug_mode'))
      ->set('postmark_debug_email', $form_state
      ->getValue('postmark_debug_email'))
      ->set('postmark_debug_no_send', $form_state
      ->getValue('postmark_debug_no_send'))
      ->save();

    // If an address is submitted, send a test email message.
    $test_address = $form_state
      ->getValue('test_address');
    if ($test_address) {
      $message = $this
        ->t('A test e-mail has been sent to @mail.  You may want to check the logs for any error messages.', [
        '@mail' => $test_address,
      ]);
      $this
        ->messenger()
        ->addWarning($message);
      $postmark_message = [
        'from' => $form_state
          ->getValue('postmark_sender_signature'),
        'to' => $test_address,
        'subject' => $this
          ->t('Test message from Postmark'),
        'text' => $this
          ->t('Just testing.'),
      ];
      $this->postmarkHandler
        ->sendMail($postmark_message);
    }
    parent::submitForm($form, $form_state);
  }

}

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.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PostmarkSettingsForm::$postmarkHandler protected property The core mail manager service.
PostmarkSettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
PostmarkSettingsForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
PostmarkSettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
PostmarkSettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
PostmarkSettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
PostmarkSettingsForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
PostmarkSettingsForm::__construct public function Constructs a Postmark settings form. Overrides ConfigFormBase::__construct
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.