diff --git a/src/Form/ConfigNotifySettingsForm.php b/src/Form/ConfigNotifySettingsForm.php index 1bb1a87e24b0bd8d4b3797b38fd8627146ef8feb..8b865ffcdc4071dd61e03881f5b0abbbb834af6e 100644 --- a/src/Form/ConfigNotifySettingsForm.php +++ b/src/Form/ConfigNotifySettingsForm.php @@ -8,6 +8,9 @@ use Drupal\Core\Url; use Drupal\Core\Link; use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\config_notify\NotifierService; +use Drupal\Core\Messenger\MessengerInterface; +use Drupal\Core\Extension\ModuleHandlerInterface; +use Drupal\Core\Datetime\DateFormatterInterface; /** * Settings form for config_notify. @@ -21,14 +24,44 @@ class ConfigNotifySettingsForm extends ConfigFormBase { */ protected $notifier; + /** + * Drupal\Core\Messenger\MessengerInterface definition. + * + * @var \Drupal\Core\Messenger\MessengerInterface + */ + protected $messenger; + + /** + * Drupal\Core\Extension\ModuleHandlerInterface definition. + * + * @var \Drupal\Core\Extension\ModuleHandlerInterface + */ + protected $moduleHandler; + + /** + * Drupal\Core\Datetime\DateFormatterInterface definition. + * + * @var \Drupal\Core\Datetime\DateFormatterInterface + */ + protected $dateFormatter; + /** * Constructs the object. * * @param \Drupal\config_notify\NotifierService $notifier * The notifier service. + * @param \Drupal\Core\Messenger\MessengerInterface $messenger + * The messenger service. + * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler + * The module handler service. + * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter + * The date formatter service. */ - public function __construct(NotifierService $notifier) { + public function __construct(NotifierService $notifier, MessengerInterface $messenger, ModuleHandlerInterface $module_handler, DateFormatterInterface $date_formatter) { $this->notifier = $notifier; + $this->messenger = $messenger; + $this->moduleHandler = $module_handler; + $this->dateFormatter = $date_formatter; } /** @@ -36,7 +69,10 @@ class ConfigNotifySettingsForm extends ConfigFormBase { */ public static function create(ContainerInterface $container) { return new static( - $container->get('config_notify.notifier') + $container->get('config_notify.notifier'), + $container->get('messenger'), + $container->get('module_handler'), + $container->get('date.formatter') ); } @@ -61,16 +97,15 @@ class ConfigNotifySettingsForm extends ConfigFormBase { */ public function buildForm(array $form, FormStateInterface $form_state) { $config = $this->config('config_notify.settings'); - $messenger = \Drupal::messenger(); $changes = $this->notifier->checkChanges(); if ($changes) { $message = $this->t('There are configuration changes.'); - $messenger->addMessage($message, 'warning'); + $this->messenger->addMessage($message, 'warning'); } else { $message = $this->t('There are no configuration changes.'); - $messenger->addMessage($message, 'status'); + $this->messenger->addMessage($message, 'status'); } $form['notification_frequency'] = [ @@ -93,7 +128,7 @@ class ConfigNotifySettingsForm extends ConfigFormBase { $last_sent = $this->notifier->getLastNotificationSent(); if ($last_sent) { - $date = \Drupal::service('date.formatter')->format($last_sent); + $date = $this->dateFormatter->format($last_sent); $form['message'] = [ '#markup' => '<p>' . '<strong>' . $this->t('Last notification was sent:') . '</strong> ' . $date . '</p>', ]; @@ -110,8 +145,7 @@ class ConfigNotifySettingsForm extends ConfigFormBase { '#default_value' => $config->get('email'), ]; - $moduleHandler = \Drupal::service('module_handler'); - if ($moduleHandler->moduleExists('slack')) { + if ($this->moduleHandler->moduleExists('slack')) { $form['slack'] = [ '#type' => 'checkbox', '#title' => $this->t('Slack notification'), @@ -173,19 +207,19 @@ class ConfigNotifySettingsForm extends ConfigFormBase { $config = $this->config('config_notify.settings'); if ($config->get('slack')) { if ($this->notifier->notifySlack($message)) { - \Drupal::messenger()->addMessage($this->t('Slack: Message was successfully sent.')); + $this->messenger->addMessage($this->t('Slack: Message was successfully sent.')); } else { - \Drupal::messenger()->addMessage($this->t('Slack: Message not sent. Please check log messages for details'), 'warning'); + $this->messenger->addMessage($this->t('Slack: Message not sent. Please check log messages for details'), 'warning'); } } if ($config->get('email')) { if ($this->notifier->notifyEmail($message)) { - \Drupal::messenger()->addMessage($this->t('Email: Message was successfully sent.')); + $this->messenger->addMessage($this->t('Email: Message was successfully sent.')); } else { - \Drupal::messenger()->addMessage($this->t('Email: Message not sent. Please check log messages for details'), 'warning'); + $this->messenger->addMessage($this->t('Email: Message not sent. Please check log messages for details'), 'warning'); } }