system_modules_submit

Versions
4.7
system_modules_submit($form_id, $edit)
5
system_modules_submit($form_id, $form_values)
6 – 7
system_modules_submit($form, &$form_state)

Submit callback; handles modules form submission.

Code

modules/system/system.admin.inc, line 923

<?php
function system_modules_submit($form, &$form_state) {
  include_once DRUPAL_ROOT . '/includes/install.inc';
  $modules = array();
  // If we're not coming from the confirmation form, build the list of modules.
  if (empty($form_state['storage'])) {
    foreach ($form_state['values']['modules'] as $group_name => $group) {
      foreach ($group as $module => $enabled) {
        $modules[$module] = array('group' => $group_name, 'enabled' => $enabled['enable']);
      }
    }
  }
  else {
    // If we are coming from the confirmation form, fetch
    // the modules out of $form_state.
    $modules = $form_state['storage']['modules'];
  }

  // Get a list of all modules, it will be used to find which module requires
  // which.
  $files = system_rebuild_module_data();

  // The modules to be enabled.
  $modules_to_be_enabled = array();
  // The modules to be disabled.
  $disable_modules = array();
  // The modules to be installed.
  $new_modules = array();
  // Modules that need to be switched on because other modules require them.
  $more_modules = array();
  // Go through each module, finding out if we should enable, install, or
  // disable it. Also, we find out if there are modules it requires that are
  // not enabled.
  foreach ($modules as $name => $module) {
    // If it's enabled, find out whether to just
    // enable it, or install it.
    if ($module['enabled']) {
      if (drupal_get_installed_schema_version($name) == SCHEMA_UNINSTALLED) {
        $new_modules[$name] = $name;
      }
      elseif (!module_exists($name)) {
        $modules_to_be_enabled[$name] = $name;
      }
      // If we're not coming from a confirmation form, search for modules the
      // new ones require and see whether there are any that additionally
      // need to be switched on.
      if (empty($form_state['storage'])) {
        foreach ($form['modules'][$module['group']][$name]['#requires'] as $requires => $v) {
          if (!$modules[$requires]['enabled']) {
            if (!isset($more_modules[$name])) {
              $more_modules[$name]['name'] = $files[$name]->info['name'];
            }
            $more_modules[$name]['requires'][$requires] = $files[$requires]->info['name'];
          }
          $modules[$requires] = array('group' => $files[$requires]->info['package'], 'enabled' => TRUE);
        }
      }
    }
  }
  // A second loop is necessary, otherwise the modules set to be enabled in the
  // previous loop would not be found.
  foreach ($modules as $name => $module) {
    if (module_exists($name) && !$module['enabled']) {
      $disable_modules[$name] = $name;
    }
  }
  if ($more_modules) {
    // If we need to switch on more modules because other modules require
    // them and they haven't confirmed, don't process the submission yet. Store
    // the form submission data needed later.
    if (!isset($form_state['values']['confirm'])) {
      $form_state['storage'] = array('more_modules' => $more_modules, 'modules' => $modules);
      $form_state['rebuild'] = TRUE;
      return;
    }
    // Otherwise, install or enable the modules.
    else {
      foreach ($form_state['storage']['more_modules'] as $info) {
        foreach ($info['requires'] as $requires => $name) {
          if (drupal_get_installed_schema_version($name) == SCHEMA_UNINSTALLED) {
            $new_modules[$name] = $name;
          }
          else {
            $modules_to_be_enabled[$name] = $name;
          }
        }
      }
    }
  }

  $old_module_list = module_list();

  // Enable the modules needing enabling.
  if (!empty($modules_to_be_enabled)) {
    $sort = array();
    foreach ($modules_to_be_enabled as $module) {
      $sort[$module] = $files[$module]->sort;
    }
    array_multisort($sort, $modules_to_be_enabled);
    module_enable($modules_to_be_enabled);
  }
  // Disable the modules that need disabling.
  if (!empty($disable_modules)) {
    $sort = array();
    foreach ($disable_modules as $module) {
      $sort[$module] = $files[$module]->sort;
    }
    array_multisort($sort, $disable_modules);
    module_disable($disable_modules);
  }

  // Install new modules.
  if (!empty($new_modules)) {
    $sort = array();
    foreach ($new_modules as $key => $module) {
      if (!drupal_check_module($module)) {
        unset($new_modules[$key]);
      }
      $sort[$module] = $files[$module]->sort;
    }
    array_multisort($sort, $new_modules);
    drupal_install_modules($new_modules);
  }

  $current_module_list = module_list(TRUE);
  if ($old_module_list != $current_module_list) {
    drupal_set_message(t('The configuration options have been saved.'));
  }

  // Clear all caches.
  registry_rebuild();
  drupal_theme_rebuild();
  node_types_rebuild();
  menu_rebuild();
  cache_clear_all('schema', 'cache');
  cache_clear_all('entity_info', 'cache');
  drupal_clear_css_cache();
  drupal_clear_js_cache();

  $form_state['redirect'] = 'admin/config/modules';

  // Notify locale module about module changes, so translations can be
  // imported. This might start a batch, and only return to the redirect
  // path after that.
  module_invoke('locale', 'system_update', $new_modules);

  // Synchronize to catch any actions that were added or removed.
  actions_synchronize();

  return;
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.