UpdateReady.php in Automatic Updates 8.2
File
src/Form/UpdateReady.php
View source
<?php
namespace Drupal\automatic_updates\Form;
use Drupal\automatic_updates\BatchProcessor;
use Drupal\automatic_updates\Updater;
use Drupal\Core\Batch\BatchBuilder;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\State\StateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class UpdateReady extends FormBase {
protected $updater;
protected $state;
public function __construct(Updater $updater, MessengerInterface $messenger, StateInterface $state) {
$this->updater = $updater;
$this
->setMessenger($messenger);
$this->state = $state;
}
public function getFormId() {
return 'automatic_updates_update_ready_form';
}
public static function create(ContainerInterface $container) {
return new static($container
->get('automatic_updates.updater'), $container
->get('messenger'), $container
->get('state'));
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['backup'] = [
'#prefix' => '<strong>',
'#markup' => $this
->t('Back up your database and site before you continue. <a href=":backup_url">Learn how</a>.', [
':backup_url' => 'https://www.drupal.org/node/22281',
]),
'#suffix' => '</strong>',
];
$form['maintenance_mode'] = [
'#title' => $this
->t('Perform updates with site in maintenance mode (strongly recommended)'),
'#type' => 'checkbox',
'#default_value' => TRUE,
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Continue'),
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$session = $this
->getRequest()
->getSession();
$session
->set('maintenance_mode', $this->state
->get('system.maintenance_mode'));
if ($form_state
->getValue('maintenance_mode') === TRUE) {
$this->state
->set('system.maintenance_mode', TRUE);
}
$batch = (new BatchBuilder())
->setTitle($this
->t('Apply updates'))
->setInitMessage($this
->t('Preparing to apply updates'))
->addOperation([
BatchProcessor::class,
'commit',
])
->addOperation([
BatchProcessor::class,
'clean',
])
->setFinishCallback([
BatchProcessor::class,
'finishCommit',
])
->toArray();
batch_set($batch);
}
}