/** * Callback for updating a theme. * * @param array $form * Nested array of form elements that comprise the form. * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. */ public static function updateTheme(array $form, FormStateInterface $form_state) { if ($theme = SystemThemeSettings::getTheme($form, $form_state)) { $update_manager = new UpdateManager($theme); $installed = []; // Iterate over all pending updates. $pending = $update_manager->getPendingUpdates(); foreach ($pending as $version => $update) { // Run the update. $result = $update->update($theme); // Update failed. Show a message and stop the process. if ($result === FALSE) { drupal_set_message(t('The update @title (@version) failed. No further updates can be processed at this time.', ['@title' => $update->getTitle(), '@version' => $version]), 'error'); break; } // Update succeeded. $installed[] = $version; } // Save the last installed schema version. if ($installed) { $installed = array_flip($installed); foreach (array_keys($installed) as $version) { $installed[$version] = new FormattableMarkup('@version - @title', ['@version' => $version, '@title' => $pending[$version]->getTitle()]); } $build = ['#theme' => 'item_list', '#items' => $installed]; drupal_set_message(t('Successfully installed the following update(s) for %theme: @installed', ['%theme' => $theme->getTitle(), '@installed' => \Drupal::service('renderer')->render($build)])); // Save the latest installed version. $theme->setSetting('schema', max(array_keys($installed))); } } }
/** * Callback for updating a theme. * * @param array $form * Nested array of form elements that comprise the form. * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. */ public static function updateTheme(array $form, FormStateInterface $form_state) { if ($theme = SystemThemeSettings::getTheme($form, $form_state)) { // Due to the fact that the batch API stores it's arguments in DB storage, // theme based objects cannot be passed as an operation argument here. // During _batch_page(), the DB item will attempt to restore the arguments // using unserialize() and the autoload fix include added below may not // yet have been invoked to register the theme namespaces. So instead, // we capture the relevant information needed to reconstruct these objects // in the batch processing callback. $theme_name = $theme->getName(); // Create an operation for each update. $operations = []; foreach ($theme->getPendingUpdates() as $update) { $operations[] = [[__CLASS__, 'batchProcessUpdate'], [$theme_name, $update->getProvider() . ':' . $update->getSchema()]]; } if ($operations) { $variables = ['@theme_title' => $theme->getTitle()]; batch_set(['operations' => $operations, 'finished' => [__CLASS__, 'batchFinished'], 'title' => t('Updating @theme_title', $variables), 'init_message' => \Drupal::translation()->formatPlural(count($operations), 'Initializing 1 theme update for @theme_title...', 'Initializing @count theme updates for @theme_title...', $variables), 'progress_message' => t('Processing update @current of @total...', $variables), 'error_message' => t('An error was encountered while attempting to update the @theme_title theme.', $variables), 'file' => Bootstrap::autoloadFixInclude()]); } } }