You are here

class AmazonSesTestForm in Amazon SES 2.0.x

Amazon SES test form.

Hierarchy

Expanded class hierarchy of AmazonSesTestForm

1 string reference to 'AmazonSesTestForm'
amazon_ses.routing.yml in ./amazon_ses.routing.yml
amazon_ses.routing.yml

File

src/Form/AmazonSesTestForm.php, line 14

Namespace

Drupal\amazon_ses\Form
View source
class AmazonSesTestForm extends AmazonSesFormBase {

  /**
   * The Mail Manager service.
   *
   * @var \Drupal\Core\Mail\MailManagerInterface
   */
  protected $mailManager;

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $currentUser;

  /**
   * The config object.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $config;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    $instance = parent::create($container);
    $instance
      ->setHandler($container
      ->get('amazon_ses.handler'))
      ->setMailManager($container
      ->get('plugin.manager.mail'))
      ->setCurrentUser($container
      ->get('current_user'))
      ->setConfig($container
      ->get('config.factory'));
    return $instance;
  }

  /**
   * Set the Mail Manager.
   *
   * @param \Drupal\Core\Mail\MailManagerInterface $mail_manager
   *   The Mail Manger object.
   *
   * @return $this
   */
  protected function setMailManager(MailManagerInterface $mail_manager) {
    $this->mailManager = $mail_manager;
    return $this;
  }

  /**
   * Set the current user.
   *
   * @param \Drupal\Core\Session\AccountProxyInterface $current_user
   *   The current user.
   *
   * @return $this
   */
  protected function setCurrentUser(AccountProxyInterface $current_user) {
    $this->currentUser = $current_user;
    return $this;
  }

  /**
   * Set the config object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory object.
   *
   * @return $this
   */
  protected function setConfig(ConfigFactoryInterface $config_factory) {
    $this->config = $config_factory
      ->get('amazon_ses.settings');
    return $this;
  }

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['to'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Email'),
      '#description' => $this
        ->t('Enter an email address to send a test mail to.'),
      '#default_value' => $this->currentUser
        ->getEmail(),
      '#required' => TRUE,
    ];
    $form['actions'] = [
      '#type' => 'actions',
    ];
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#button_type' => 'primary',
      '#value' => $this
        ->t('Send'),
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $to = $form_state
      ->getValue('to');
    $from = $this->config
      ->get('from_address');
    $body = $this
      ->t('This is a test of the Amazon SES module. The module has
      been configured successfully!');
    $message = [
      'to' => $to,
      'from' => $from,
      'subject' => $this
        ->t('Amazon SES test'),
      'body' => $body,
    ];
    $message_id = $this->handler
      ->send($message);
    if ($message_id) {
      $this
        ->messenger()
        ->addMessage($this
        ->t('A test message was sent to %to.', [
        '%to' => $to,
      ]));
    }
    else {
      $this
        ->messenger()
        ->addError($this
        ->t('Error sending message to %to.', [
        '%to' => $to,
      ]));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AmazonSesHandlerTrait::$handler protected property The Amazon SES handler service.
AmazonSesHandlerTrait::setHandler protected function Set the handler object.
AmazonSesTestForm::$config protected property The config object.
AmazonSesTestForm::$currentUser protected property The current user.
AmazonSesTestForm::$mailManager protected property The Mail Manager service.
AmazonSesTestForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
AmazonSesTestForm::create public static function Instantiates a new instance of this class. Overrides AmazonSesFormBase::create
AmazonSesTestForm::getEditableConfigNames protected function
AmazonSesTestForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
AmazonSesTestForm::setConfig protected function Set the config object.
AmazonSesTestForm::setCurrentUser protected function Set the current user.
AmazonSesTestForm::setMailManager protected function Set the Mail Manager.
AmazonSesTestForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 3
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 3
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.
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.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 72
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. 27
MessengerTrait::messenger public function Gets the messenger. 27
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. 4
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.