You are here

class SettingsForm in Notify 8

Same name and namespace in other branches
  1. 2.0.x src/Form/SettingsForm.php \Drupal\notify\Form\SettingsForm
  2. 1.0.x src/Form/SettingsForm.php \Drupal\notify\Form\SettingsForm

Defines a form that configures forms module settings.

Hierarchy

Expanded class hierarchy of SettingsForm

1 string reference to 'SettingsForm'
notify.routing.yml in ./notify.routing.yml
notify.routing.yml

File

src/Form/SettingsForm.php, line 20

Namespace

Drupal\notify\Form
View source
class SettingsForm extends ConfigFormBase {

  /**
   * Drupal\Core\Session\AccountProxyInterface definition.
   *
   * @var AccountProxyInterface $currentUser
   */
  protected $currentUser;

  /**
   * Drupal\Core\Messenger\MessengerInterface definition.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected $messenger;

  /**
   * Class constructor.
   *
   * @param ConfigFactoryInterface $config_factory
   *   The config factory.
   * @param AccountProxyInterface $current_user
   *   The current user.
   * @param MessengerInterface $messenger
   *   The core messenger service.
   */
  public function __construct(ConfigFactoryInterface $config_factory, AccountProxyInterface $current_user, MessengerInterface $messenger) {
    parent::__construct($config_factory);
    $this->currentUser = $current_user;
    $this->messenger = $messenger;
  }

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

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL) {
    $config = $this
      ->config('notify.settings');
    $period = [
      300 => \Drupal::service('date.formatter')
        ->formatInterval(300),
      600 => \Drupal::service('date.formatter')
        ->formatInterval(600),
      900 => \Drupal::service('date.formatter')
        ->formatInterval(900),
      1800 => \Drupal::service('date.formatter')
        ->formatInterval(1800),
      3600 => \Drupal::service('date.formatter')
        ->formatInterval(3600),
      10800 => \Drupal::service('date.formatter')
        ->formatInterval(10800),
      21600 => \Drupal::service('date.formatter')
        ->formatInterval(21600),
      43200 => \Drupal::service('date.formatter')
        ->formatInterval(43200),
      86400 => \Drupal::service('date.formatter')
        ->formatInterval(86400),
      172800 => \Drupal::service('date.formatter')
        ->formatInterval(172800),
      259200 => \Drupal::service('date.formatter')
        ->formatInterval(259200),
      604800 => \Drupal::service('date.formatter')
        ->formatInterval(604800),
      1209600 => \Drupal::service('date.formatter')
        ->formatInterval(1209600),
      2419200 => \Drupal::service('date.formatter')
        ->formatInterval(2419200),
      -1 => $this
        ->t('Never'),
    ];
    $attempts = [
      0 => $this
        ->t('Disabled'),
      1 => 1,
      2 => 2,
      3 => 3,
      4 => 4,
      5 => 5,
      6 => 6,
      7 => 7,
      8 => 8,
      9 => 9,
      10 => 10,
      15 => 15,
      20 => 20,
    ];
    $batch = [
      2 => 2,
      3 => 3,
      10 => 10,
      20 => 20,
      50 => 50,
      100 => 100,
      200 => 200,
      400 => 400,
    ];
    $form['notify_settings'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('E-mail notification settings'),
      '#collapsible' => TRUE,
    ];
    $form['notify_settings']['notify_period'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Send notifications every'),
      '#default_value' => $config
        ->get('notify_period'),
      '#options' => $period,
      '#description' => $this
        ->t('How often should new content notifications be sent? Requires cron to be running at least this often.'),
    ];
    $form['notify_settings']['notify_send_hour'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Hour to Send Notifications'),
      '#description' => $this
        ->t('Specify the hour (24-hour clock) in which notifications should be sent, if the frequency is one day or greater.'),
      '#default_value' => $config
        ->get('notify_send_hour'),
      '#options' => [
        0,
        1,
        2,
        3,
        4,
        5,
        6,
        7,
        8,
        9,
        10,
        11,
        12,
        13,
        14,
        15,
        16,
        17,
        18,
        19,
        20,
        21,
        22,
        23,
      ],
    ];
    $form['notify_settings']['notify_attempts'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Number of failed sends after which notifications are disabled'),
      '#default_value' => $config
        ->get('notify_attempts'),
      '#options' => $attempts,
    ];
    $form['notify_settings']['notify_batch'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Maximum number of notifications to send out per cron run'),
      '#description' => $this
        ->t('The maximum number of notification e-mails to send in each pass of  a cron maintenance task. If necessary, reduce the number of items to prevent resource limit conflicts.'),
      '#default_value' => $config
        ->get('notify_batch'),
      '#options' => $batch,
    ];
    $form['notify_settings']['notify_include_updates'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Include updated posts in notifications'),
      '#return_value' => 1,
      '#default_value' => $config
        ->get('notify_include_updates'),
    ];
    $form['notify_settings']['notify_unpublished'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Administrators shall be notified about unpublished content of tracked types'),
      '#return_value' => 1,
      '#default_value' => $config
        ->get('notify_unpublished'),
    ];
    $form['notify_settings']['notify_watchdog'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('Watchdog log level'),
      '#default_value' => $config
        ->get('notify_watchdog'),
      '#options' => [
        $this
          ->t('All'),
        $this
          ->t('Failures+Summary'),
        $this
          ->t('Failures'),
        $this
          ->t('Nothing'),
      ],
      '#description' => $this
        ->t('This setting lets you specify how much to log.'),
    ];
    $form['notify_settings']['notify_weightur'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Weight of notification field in user registration form'),
      '#default_value' => $config
        ->get('notify_weightur'),
      '#size' => 3,
      '#maxlength' => 5,
      '#description' => $this
        ->t('The weight you set here will determine the position of the notification field when it appears in the user registration form.'),
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state
      ->getValues();
    $this
      ->config('notify.settings')
      ->set('notify_period', $values['notify_period'])
      ->set('notify_send_hour', $values['notify_send_hour'])
      ->set('notify_attempts', $values['notify_attempts'])
      ->set('notify_batch', $values['notify_batch'])
      ->set('notify_include_updates', $values['notify_include_updates'])
      ->set('notify_unpublished', $values['notify_unpublished'])
      ->set('notify_watchdog', $values['notify_watchdog'])
      ->set('notify_weightur', $values['notify_weightur'])
      ->save();
    $this->messenger
      ->addMessage($this
      ->t('Notify admin settings 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.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
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 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.
SettingsForm::$currentUser protected property Drupal\Core\Session\AccountProxyInterface definition.
SettingsForm::$messenger protected property Drupal\Core\Messenger\MessengerInterface definition. Overrides MessengerTrait::$messenger
SettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
SettingsForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
SettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
SettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
SettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
SettingsForm::__construct public function Class constructor. Overrides ConfigFormBase::__construct
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.