3 namespace Drupal\update\Form;
5 use Drupal\Core\Extension\ModuleHandlerInterface;
6 use Drupal\Core\FileTransfer\Local;
7 use Drupal\Core\Form\FormBase;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\State\StateInterface;
10 use Drupal\Core\Updater\Updater;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12 use Symfony\Component\HttpFoundation\Response;
15 * Configure update settings for this site.
17 class UpdateReady extends FormBase {
20 * The root location under which updated projects will be saved.
29 * @var \Drupal\Core\Extension\ModuleHandlerInterface
31 protected $moduleHandler;
34 * The state key value store.
36 * @var \Drupal\Core\State\StateInterface
48 * Constructs a new UpdateReady object.
51 * The root location under which updated projects will be saved.
52 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
53 * The object that manages enabled modules in a Drupal installation.
54 * @param \Drupal\Core\State\StateInterface $state
55 * The state key value store.
56 * @param string $site_path
59 public function __construct($root, ModuleHandlerInterface $module_handler, StateInterface $state, $site_path) {
61 $this->moduleHandler = $module_handler;
62 $this->state = $state;
63 $this->sitePath = $site_path;
69 public function getFormId() {
70 return 'update_manager_update_ready_form';
76 public static function create(ContainerInterface $container) {
78 $container->get('update.root'),
79 $container->get('module_handler'),
80 $container->get('state'),
81 $container->get('site.path')
88 public function buildForm(array $form, FormStateInterface $form_state) {
89 $this->moduleHandler->loadInclude('update', 'inc', 'update.manager');
90 if (!_update_manager_check_backends($form, 'update')) {
95 '#prefix' => '<strong>',
96 '#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']),
97 '#suffix' => '</strong>',
100 $form['maintenance_mode'] = [
101 '#title' => $this->t('Perform updates with site in maintenance mode (strongly recommended)'),
102 '#type' => 'checkbox',
103 '#default_value' => TRUE,
106 $form['actions'] = ['#type' => 'actions'];
107 $form['actions']['submit'] = [
109 '#value' => $this->t('Continue'),
118 public function submitForm(array &$form, FormStateInterface $form_state) {
119 // Store maintenance_mode setting so we can restore it when done.
120 $_SESSION['maintenance_mode'] = $this->state->get('system.maintenance_mode');
121 if ($form_state->getValue('maintenance_mode') == TRUE) {
122 $this->state->set('system.maintenance_mode', TRUE);
125 if (!empty($_SESSION['update_manager_update_projects'])) {
126 // Make sure the Updater registry is loaded.
127 drupal_get_updaters();
130 $directory = _update_manager_extract_directory();
132 $projects = $_SESSION['update_manager_update_projects'];
133 unset($_SESSION['update_manager_update_projects']);
135 $project_real_location = NULL;
136 foreach ($projects as $project => $url) {
137 $project_location = $directory . '/' . $project;
138 $updater = Updater::factory($project_location, $this->root);
139 $project_real_location = drupal_realpath($project_location);
141 'project' => $project,
142 'updater_name' => get_class($updater),
143 'local_url' => $project_real_location,
147 // If the owner of the last directory we extracted is the same as the
148 // owner of our configuration directory (e.g. sites/default) where we're
149 // trying to install the code, there's no need to prompt for FTP/SSH
150 // credentials. Instead, we instantiate a Drupal\Core\FileTransfer\Local
151 // and invoke update_authorize_run_update() directly.
152 if (fileowner($project_real_location) == fileowner($this->sitePath)) {
153 $this->moduleHandler->loadInclude('update', 'inc', 'update.authorize');
154 $filetransfer = new Local($this->root);
155 $response = update_authorize_run_update($filetransfer, $updates);
156 if ($response instanceof Response) {
157 $form_state->setResponse($response);
160 // Otherwise, go through the regular workflow to prompt for FTP/SSH
161 // credentials and invoke update_authorize_run_update() indirectly with
162 // whatever FileTransfer object authorize.php creates for us.
164 // The page title must be passed here to ensure it is initially used
165 // when authorize.php loads for the first time with the FTP/SSH
167 system_authorized_init('update_authorize_run_update', __DIR__ . '/../../update.authorize.inc', [$updates], $this->t('Update manager'));
168 $form_state->setRedirectUrl(system_authorized_get_url());