You are here

class QueueMailSettingsForm in Queue Mail 8

Configures module's settings.

Hierarchy

Expanded class hierarchy of QueueMailSettingsForm

1 string reference to 'QueueMailSettingsForm'
queue_mail.routing.yml in ./queue_mail.routing.yml
queue_mail.routing.yml

File

src/Form/QueueMailSettingsForm.php, line 16

Namespace

Drupal\queue_mail\Form
View source
class QueueMailSettingsForm extends ConfigFormBase {

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * The redirect destination.
   *
   * @var \Drupal\Core\Routing\RedirectDestinationInterface
   */
  protected $redirectDestination;

  /**
   * Constructs a new QueueMailSettingsForm.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   Module handler service.
   * @param Drupal\Core\Routing\RedirectDestinationInterface $redirect_destination
   *   The redirect destination.
   */
  public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler, RedirectDestinationInterface $redirect_destination) {
    parent::__construct($config_factory);
    $this->moduleHandler = $module_handler;
    $this->redirectDestination = $redirect_destination;
  }

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

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this->configFactory
      ->get('queue_mail.settings');
    $form['queue_status'] = [
      '#type' => 'item',
      '#title' => $this
        ->t('Queue status'),
      '#markup' => $this
        ->formatPlural(_queue_mail_get_queue()
        ->numberOfItems(), '1 mail currently queued for sending.', '@count mails currently queued for sending.'),
      '#description' => $this
        ->t('Sending of queued mails happens on cron. You can <a href="@cron_link">run cron manually</a>.', [
        '@cron_link' => Url::fromRoute('system.run_cron', [], [
          'query' => $this->redirectDestination
            ->getAsArray(),
        ])
          ->toString(),
      ]),
    ];
    $form['queue_mail_keys'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Mail IDs to queue'),
      '#description' => $this
        ->t('Enter each mail ID to queue on a separate line. Use <strong>*</strong> to do a wildcard match.') . '<br/>' . $this
        ->t('Mail IDs are a combination of the first and second arguments sent to <em>drupal_mail</em> when a module sends an email. E.g. <em>user_mail</em>, <em>register_pending_approval_admin</em>') . '<br />' . $this
        ->t('For example, to queue all mails sent by the User module, enter: <em>user_*</em> above, to just queue password recovery emails enter: <em>user_password_reset</em>'),
      '#default_value' => $config
        ->get('queue_mail_keys'),
    ];

    // Get a list of modules that implement hook_mail.
    $mail_modules = $this->moduleHandler
      ->getImplementations('mail');
    $rows = [];
    foreach ($mail_modules as $name) {
      $row = [];
      $row[] = $name;
      $row[] = $name . '_*';
      $rows[] = $row;
    }
    $headers = [
      $this
        ->t('Module name'),
      $this
        ->t('Mail ID prefix'),
    ];
    $form['tables'] = [
      '#type' => 'table',
      '#caption' => $this
        ->t('The following modules send emails. The contents of the second column can be used in the options above to queue the sending of those emails.'),
      '#header' => $headers,
      '#rows' => $rows,
      '#empty' => $this
        ->t('No tables available.'),
    ];
    $form['advanced'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Advanced settings'),
      '#collapsed' => TRUE,
    ];
    $options = [];
    for ($i = 5; $i <= 240; $i += 5) {
      $options[$i] = $this
        ->formatPlural($i, '1 second', '@count seconds');
    }
    $form['advanced']['queue_mail_queue_time'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Queue processing time (max)'),
      '#description' => $this
        ->t('How much time in seconds to allow queue mail to send emails for on cron. Warning if you set a very high limit your cron run could timeout and never complete.'),
      '#options' => $options,
      '#default_value' => $config
        ->get('queue_mail_queue_time'),
    ];
    $form['advanced']['threshold'] = [
      '#type' => 'number',
      '#min' => 0,
      '#title' => $this
        ->t('Queue retry threshold'),
      '#default_value' => $config
        ->get('threshold'),
      '#description' => $this
        ->t('How many times to retry before deleting an item.'),
    ];
    $form['advanced']['requeue_interval'] = [
      '#type' => 'number',
      '#min' => 0,
      '#title' => $this
        ->t('Requeue interval'),
      '#default_value' => $config
        ->get('requeue_interval'),
      '#field_suffix' => $this
        ->t('seconds'),
      '#description' => $this
        ->t('How long to wait in seconds before retrying failed emails.'),
    ];
    $form['advanced']['queue_mail_queue_wait_time'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Wait time per item'),
      '#description' => $this
        ->t('How long to wait in seconds in between the processing of each item. This value can not be bigger than "Queue processing time" value.'),
      '#step' => 1,
      '#min' => 0,
      '#field_suffix' => $this
        ->t('seconds'),
      '#default_value' => $config
        ->get('queue_mail_queue_wait_time') ?: 0,
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $queue_mail_queue_time = $form_state
      ->getValue('queue_mail_queue_time');
    $queue_mail_queue_wait_time = $form_state
      ->getValue('queue_mail_queue_wait_time');
    if ($queue_mail_queue_time < $queue_mail_queue_wait_time) {
      $form_state
        ->setErrorByName('queue_mail_queue_wait_time', $this
        ->t('"Wait time per item" value can not be bigger than "Queue processing time" value.'));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this
      ->config('queue_mail.settings')
      ->set('queue_mail_keys', $form_state
      ->getValue('queue_mail_keys'))
      ->set('queue_mail_queue_time', $form_state
      ->getValue('queue_mail_queue_time'))
      ->set('threshold', $form_state
      ->getValue('threshold'))
      ->set('requeue_interval', $form_state
      ->getValue('requeue_interval'))
      ->set('queue_mail_queue_wait_time', $form_state
      ->getValue('queue_mail_queue_wait_time'))
      ->save();
    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.
QueueMailSettingsForm::$moduleHandler protected property The module handler.
QueueMailSettingsForm::$redirectDestination protected property The redirect destination. Overrides RedirectDestinationTrait::$redirectDestination
QueueMailSettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
QueueMailSettingsForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
QueueMailSettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
QueueMailSettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
QueueMailSettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
QueueMailSettingsForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
QueueMailSettingsForm::__construct public function Constructs a new QueueMailSettingsForm. Overrides ConfigFormBase::__construct
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.