3 namespace Drupal\update;
5 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
6 use Symfony\Component\DependencyInjection\ContainerInterface;
7 use Drupal\Core\Form\ConfigFormBase;
8 use Drupal\Core\Form\FormStateInterface;
9 use Egulias\EmailValidator\EmailValidator;
12 * Configure update settings for this site.
14 class UpdateSettingsForm extends ConfigFormBase implements ContainerInjectionInterface {
17 * The email validator.
19 * @var \Egulias\EmailValidator\EmailValidator
21 protected $emailValidator;
24 * Constructs a new UpdateSettingsForm.
26 * @param \Egulias\EmailValidator\EmailValidator $email_validator
27 * The email validator.
29 public function __construct(EmailValidator $email_validator) {
30 $this->emailValidator = $email_validator;
36 public static function create(ContainerInterface $container) {
38 $container->get('email.validator')
45 public function getFormId() {
46 return 'update_settings';
52 protected function getEditableConfigNames() {
53 return ['update.settings'];
59 public function buildForm(array $form, FormStateInterface $form_state) {
60 $config = $this->config('update.settings');
62 $form['update_check_frequency'] = [
64 '#title' => t('Check for updates'),
65 '#default_value' => $config->get('check.interval_days'),
70 '#description' => t('Select how frequently you want to automatically check for new releases of your currently installed modules and themes.'),
73 $form['update_check_disabled'] = [
74 '#type' => 'checkbox',
75 '#title' => t('Check for updates of uninstalled modules and themes'),
76 '#default_value' => $config->get('check.disabled_extensions'),
79 $notification_emails = $config->get('notification.emails');
80 $form['update_notify_emails'] = [
81 '#type' => 'textarea',
82 '#title' => t('Email addresses to notify when updates are available'),
84 '#default_value' => implode("\n", $notification_emails),
85 '#description' => t('Whenever your site checks for available updates and finds new releases, it can notify a list of users via email. Put each address on a separate line. If blank, no emails will be sent.'),
88 $form['update_notification_threshold'] = [
90 '#title' => t('Email notification threshold'),
91 '#default_value' => $config->get('notification.threshold'),
93 'all' => t('All newer versions'),
94 'security' => t('Only security updates'),
96 '#description' => t('You can choose to send email only if a security update is available, or to be notified about all newer versions. If there are updates available of Drupal core or any of your installed modules and themes, your site will always print a message on the <a href=":status_report">status report</a> page, and will also display an error message on administration pages if there is a security update.', [':status_report' => $this->url('system.status')])
99 return parent::buildForm($form, $form_state);
105 public function validateForm(array &$form, FormStateInterface $form_state) {
106 $form_state->set('notify_emails', []);
107 if (!$form_state->isValueEmpty('update_notify_emails')) {
110 foreach (explode("\n", trim($form_state->getValue('update_notify_emails'))) as $email) {
111 $email = trim($email);
112 if (!empty($email)) {
113 if ($this->emailValidator->isValid($email)) {
121 if (empty($invalid)) {
122 $form_state->set('notify_emails', $valid);
124 elseif (count($invalid) == 1) {
125 $form_state->setErrorByName('update_notify_emails', $this->t('%email is not a valid email address.', ['%email' => reset($invalid)]));
128 $form_state->setErrorByName('update_notify_emails', $this->t('%emails are not valid email addresses.', ['%emails' => implode(', ', $invalid)]));
132 parent::validateForm($form, $form_state);
138 public function submitForm(array &$form, FormStateInterface $form_state) {
139 $config = $this->config('update.settings');
140 // See if the update_check_disabled setting is being changed, and if so,
141 // invalidate all update status data.
142 if ($form_state->getValue('update_check_disabled') != $config->get('check.disabled_extensions')) {
143 update_storage_clear();
147 ->set('check.disabled_extensions', $form_state->getValue('update_check_disabled'))
148 ->set('check.interval_days', $form_state->getValue('update_check_frequency'))
149 ->set('notification.emails', $form_state->get('notify_emails'))
150 ->set('notification.threshold', $form_state->getValue('update_notification_threshold'))
153 parent::submitForm($form, $form_state);