/** * {@inheritdoc} */ public function install(array $module_list, $enable_dependencies = TRUE) { $extension_config = \Drupal::config('core.extension'); if ($enable_dependencies) { // Get all module data so we can find dependencies and sort. $module_data = system_rebuild_module_data(); $module_list = $module_list ? array_combine($module_list, $module_list) : array(); if (array_diff_key($module_list, $module_data)) { // One or more of the given modules doesn't exist. return FALSE; } // Only process currently uninstalled modules. $installed_modules = $extension_config->get('module') ?: array(); if (!($module_list = array_diff_key($module_list, $installed_modules))) { // Nothing to do. All modules already installed. return TRUE; } // Add dependencies to the list. The new modules will be processed as // the while loop continues. while (list($module) = each($module_list)) { foreach (array_keys($module_data[$module]->requires) as $dependency) { if (!isset($module_data[$dependency])) { // The dependency does not exist. return FALSE; } // Skip already installed modules. if (!isset($module_list[$dependency]) && !isset($installed_modules[$dependency])) { $module_list[$dependency] = $dependency; } } } // Set the actual module weights. $module_list = array_map(function ($module) use($module_data) { return $module_data[$module]->sort; }, $module_list); // Sort the module list by their weights (reverse). arsort($module_list); $module_list = array_keys($module_list); } // Required for module installation checks. include_once DRUPAL_ROOT . '/core/includes/install.inc'; /** @var \Drupal\Core\Config\ConfigInstaller $config_installer */ $config_installer = \Drupal::service('config.installer'); $sync_status = $config_installer->isSyncing(); if ($sync_status) { $source_storage = $config_installer->getSourceStorage(); } $modules_installed = array(); foreach ($module_list as $module) { $enabled = $extension_config->get("module.{$module}") !== NULL; if (!$enabled) { // Throw an exception if the module name is too long. if (strlen($module) > DRUPAL_EXTENSION_NAME_MAX_LENGTH) { throw new ExtensionNameLengthException(format_string('Module name %name is over the maximum allowed length of @max characters.', array('%name' => $module, '@max' => DRUPAL_EXTENSION_NAME_MAX_LENGTH))); } $extension_config->set("module.{$module}", 0)->set('module', module_config_sort($extension_config->get('module')))->save(); // Prepare the new module list, sorted by weight, including filenames. // This list is used for both the ModuleHandler and DrupalKernel. It // needs to be kept in sync between both. A DrupalKernel reboot or // rebuild will automatically re-instantiate a new ModuleHandler that // uses the new module list of the kernel. However, DrupalKernel does // not cause any modules to be loaded. // Furthermore, the currently active (fixed) module list can be // different from the configured list of enabled modules. For all active // modules not contained in the configured enabled modules, we assume a // weight of 0. $current_module_filenames = $this->getModuleList(); $current_modules = array_fill_keys(array_keys($current_module_filenames), 0); $current_modules = module_config_sort(array_merge($current_modules, $extension_config->get('module'))); $module_filenames = array(); foreach ($current_modules as $name => $weight) { if (isset($current_module_filenames[$name])) { $module_filenames[$name] = $current_module_filenames[$name]; } else { $module_path = drupal_get_path('module', $name); $pathname = "{$module_path}/{$name}.info.yml"; $filename = file_exists($module_path . "/{$name}.module") ? "{$name}.module" : NULL; $module_filenames[$name] = new Extension('module', $pathname, $filename); } } // Update the module handler in order to load the module's code. // This allows the module to participate in hooks and its existence to // be discovered by other modules. // The current ModuleHandler instance is obsolete with the kernel // rebuild below. $this->setModuleList($module_filenames); $this->load($module); module_load_install($module); // Clear the static cache of system_rebuild_module_data() to pick up the // new module, since it merges the installation status of modules into // its statically cached list. drupal_static_reset('system_rebuild_module_data'); // Update the kernel to include it. // This reboots the kernel to register the module's bundle and its // services in the service container. The $module_filenames argument is // taken over as %container.modules% parameter, which is passed to a // fresh ModuleHandler instance upon first retrieval. // @todo install_begin_request() creates a container without a kernel. if ($kernel = \Drupal::service('kernel', ContainerInterface::NULL_ON_INVALID_REFERENCE)) { $kernel->updateModules($module_filenames, $module_filenames); } // Refresh the schema to include it. drupal_get_schema(NULL, TRUE); // Allow modules to react prior to the installation of a module. $this->invokeAll('module_preinstall', array($module)); // Now install the module's schema if necessary. drupal_install_schema($module); // Clear plugin manager caches and flag router to rebuild if requested. \Drupal::getContainer()->get('plugin.cache_clearer')->clearCachedDefinitions(); \Drupal::service('router.builder_indicator')->setRebuildNeeded(); // Set the schema version to the number of the last update provided by // the module, or the minimum core schema version. $version = \Drupal::CORE_MINIMUM_SCHEMA_VERSION; $versions = drupal_get_schema_versions($module); if ($versions) { $version = max(max($versions), $version); } // Notify the entity manager that this module's entity types are new, // so that it can notify all interested handlers. For example, a // SQL-based storage handler can use this as an opportunity to create // the necessary database tables. $entity_manager = \Drupal::entityManager(); foreach ($entity_manager->getDefinitions() as $entity_type) { if ($entity_type->getProvider() == $module) { $entity_manager->onEntityTypeCreate($entity_type); } } // Install default configuration of the module. $config_installer = \Drupal::service('config.installer'); if ($sync_status) { $config_installer->setSyncing(TRUE)->setSourceStorage($source_storage); } else { // If we're not in a config synchronisation reset the source storage // so that the extension install storage will pick up the new // configuration. $config_installer->resetSourceStorage(); } \Drupal::service('config.installer')->installDefaultConfig('module', $module); // If the module has no current updates, but has some that were // previously removed, set the version to the value of // hook_update_last_removed(). if ($last_removed = $this->invoke($module, 'update_last_removed')) { $version = max($version, $last_removed); } drupal_set_installed_schema_version($module, $version); // Record the fact that it was installed. $modules_installed[] = $module; // file_get_stream_wrappers() needs to re-register Drupal's stream // wrappers in case a module-provided stream wrapper is used later in // the same request. In particular, this happens when installing Drupal // via Drush, as the 'translations' stream wrapper is provided by // Interface Translation module and is later used to import // translations. \Drupal::service('stream_wrapper_manager')->register(); // Update the theme registry to include it. drupal_theme_rebuild(); // Modules can alter theme info, so refresh theme data. // @todo ThemeHandler cannot be injected into ModuleHandler, since that // causes a circular service dependency. // @see https://drupal.org/node/2208429 \Drupal::service('theme_handler')->refreshInfo(); // Allow the module to perform install tasks. $this->invoke($module, 'install'); // Record the fact that it was installed. \Drupal::logger('system')->info('%module module installed.', array('%module' => $module)); } } // If any modules were newly installed, invoke hook_modules_installed(). if (!empty($modules_installed)) { $this->invokeAll('modules_installed', array($modules_installed)); } return TRUE; }
protected function filterOutIgnored($data, $originalData) { foreach ($this->adjustments as $module) { if (is_array($originalData) && array_key_exists($module, $originalData['module'])) { $data['module'][$module] = $originalData['module'][$module]; } else { unset($data['module'][$module]); } } // Make sure data stays sorted so that == comparison works. $data['module'] = module_config_sort($data['module']); return $data; }
/** * Tests importing configuration. */ function testImport() { $name = 'system.site'; $dynamic_name = 'config_test.dynamic.new'; /** @var \Drupal\Core\Config\StorageInterface $staging */ $staging = $this->container->get('config.storage.staging'); $this->drupalGet('admin/config/development/configuration'); $this->assertText('There are no configuration changes to import.'); $this->assertNoFieldById('edit-submit', t('Import all')); // Create updated configuration object. $new_site_name = 'Config import test ' . $this->randomString(); $this->prepareSiteNameUpdate($new_site_name); $this->assertIdentical($staging->exists($name), TRUE, $name . ' found.'); // Create new config entity. $original_dynamic_data = array('uuid' => '30df59bd-7b03-4cf7-bb35-d42fc49f0651', 'langcode' => \Drupal::languageManager()->getDefaultLanguage()->getId(), 'status' => TRUE, 'dependencies' => array(), 'id' => 'new', 'label' => 'New', 'weight' => 0, 'style' => '', 'size' => '', 'size_value' => '', 'protected_property' => ''); $staging->write($dynamic_name, $original_dynamic_data); $this->assertIdentical($staging->exists($dynamic_name), TRUE, $dynamic_name . ' found.'); // Enable the Action and Ban modules during import. The Ban // module is used because it creates a table during the install. The Action // module is used because it creates a single simple configuration file // during the install. $core_extension = $this->config('core.extension')->get(); $core_extension['module']['action'] = 0; $core_extension['module']['ban'] = 0; $core_extension['module'] = module_config_sort($core_extension['module']); // Bartik is a subtheme of classy so classy must be enabled. $core_extension['theme']['classy'] = 0; $core_extension['theme']['bartik'] = 0; $staging->write('core.extension', $core_extension); // Use the install storage so that we can read configuration from modules // and themes that are not installed. $install_storage = new InstallStorage(); // Set the Bartik theme as default. $system_theme = $this->config('system.theme')->get(); $system_theme['default'] = 'bartik'; $staging->write('system.theme', $system_theme); // Read the action config from module default config folder. $action_settings = $install_storage->read('action.settings'); $action_settings['recursion_limit'] = 50; $staging->write('action.settings', $action_settings); // Uninstall the Options and Text modules to ensure that dependencies are // handled correctly. Options depends on Text so Text should be installed // first. Since they were enabled during the test setup the core.extension // file in staging will already contain them. \Drupal::service('module_installer')->uninstall(array('text', 'options')); // Set the state system to record installations and uninstallations. \Drupal::state()->set('ConfigImportUITest.core.extension.modules_installed', array()); \Drupal::state()->set('ConfigImportUITest.core.extension.modules_uninstalled', array()); // Verify that both appear as ready to import. $this->drupalGet('admin/config/development/configuration'); $this->assertRaw('<td>' . $name); $this->assertRaw('<td>' . $dynamic_name); $this->assertRaw('<td>core.extension'); $this->assertRaw('<td>system.theme'); $this->assertRaw('<td>action.settings'); $this->assertFieldById('edit-submit', t('Import all')); // Import and verify that both do not appear anymore. $this->drupalPostForm(NULL, array(), t('Import all')); $this->assertNoRaw('<td>' . $name); $this->assertNoRaw('<td>' . $dynamic_name); $this->assertNoRaw('<td>core.extension'); $this->assertNoRaw('<td>system.theme'); $this->assertNoRaw('<td>action.settings'); $this->assertNoFieldById('edit-submit', t('Import all')); // Verify that there are no further changes to import. $this->assertText(t('There are no configuration changes to import.')); // Verify site name has changed. $this->assertIdentical($new_site_name, $this->config('system.site')->get('name')); // Verify that new config entity exists. $this->assertIdentical($original_dynamic_data, $this->config($dynamic_name)->get()); // Verify the cache got cleared. $this->assertTrue(isset($GLOBALS['hook_cache_flush'])); $this->rebuildContainer(); $this->assertTrue(\Drupal::moduleHandler()->moduleExists('ban'), 'Ban module installed during import.'); $this->assertTrue(\Drupal::database()->schema()->tableExists('ban_ip'), 'The database table ban_ip exists.'); $this->assertTrue(\Drupal::moduleHandler()->moduleExists('action'), 'Action module installed during import.'); $this->assertTrue(\Drupal::moduleHandler()->moduleExists('options'), 'Options module installed during import.'); $this->assertTrue(\Drupal::moduleHandler()->moduleExists('text'), 'Text module installed during import.'); $theme_info = \Drupal::service('theme_handler')->listInfo(); $this->assertTrue($theme_info['bartik']->status, 'Bartik theme installed during import.'); // Ensure installations and uninstallation occur as expected. $installed = \Drupal::state()->get('ConfigImportUITest.core.extension.modules_installed', array()); $uninstalled = \Drupal::state()->get('ConfigImportUITest.core.extension.modules_uninstalled', array()); $expected = array('action', 'ban', 'text', 'options'); $this->assertIdentical($expected, $installed, 'Action, Ban, Text and Options modules installed in the correct order.'); $this->assertTrue(empty($uninstalled), 'No modules uninstalled during import'); // Verify that the action.settings configuration object was only written // once during the import process and only with the value set in the staged // configuration. This verifies that the module's default configuration is // used during configuration import and, additionally, that after installing // a module, that configuration is not synced twice. $recursion_limit_values = \Drupal::state()->get('ConfigImportUITest.action.settings.recursion_limit', array()); $this->assertIdentical($recursion_limit_values, array(50)); $core_extension = $this->config('core.extension')->get(); unset($core_extension['module']['action']); unset($core_extension['module']['ban']); unset($core_extension['module']['options']); unset($core_extension['module']['text']); unset($core_extension['theme']['bartik']); $staging->write('core.extension', $core_extension); $staging->delete('action.settings'); $staging->delete('text.settings'); $system_theme = $this->config('system.theme')->get(); $system_theme['default'] = 'stark'; $system_theme['admin'] = 'stark'; $staging->write('system.theme', $system_theme); // Set the state system to record installations and uninstallations. \Drupal::state()->set('ConfigImportUITest.core.extension.modules_installed', array()); \Drupal::state()->set('ConfigImportUITest.core.extension.modules_uninstalled', array()); // Verify that both appear as ready to import. $this->drupalGet('admin/config/development/configuration'); $this->assertRaw('<td>core.extension'); $this->assertRaw('<td>system.theme'); $this->assertRaw('<td>action.settings'); // Import and verify that both do not appear anymore. $this->drupalPostForm(NULL, array(), t('Import all')); $this->assertNoRaw('<td>core.extension'); $this->assertNoRaw('<td>system.theme'); $this->assertNoRaw('<td>action.settings'); $this->rebuildContainer(); $this->assertFalse(\Drupal::moduleHandler()->moduleExists('ban'), 'Ban module uninstalled during import.'); $this->assertFalse(\Drupal::database()->schema()->tableExists('ban_ip'), 'The database table ban_ip does not exist.'); $this->assertFalse(\Drupal::moduleHandler()->moduleExists('action'), 'Action module uninstalled during import.'); $this->assertFalse(\Drupal::moduleHandler()->moduleExists('options'), 'Options module uninstalled during import.'); $this->assertFalse(\Drupal::moduleHandler()->moduleExists('text'), 'Text module uninstalled during import.'); // Ensure installations and uninstallation occur as expected. $installed = \Drupal::state()->get('ConfigImportUITest.core.extension.modules_installed', array()); $uninstalled = \Drupal::state()->get('ConfigImportUITest.core.extension.modules_uninstalled', array()); $expected = array('options', 'text', 'ban', 'action'); $this->assertIdentical($expected, $uninstalled, 'Options, Text, Ban and Action modules uninstalled in the correct order.'); $this->assertTrue(empty($installed), 'No modules installed during import'); $theme_info = \Drupal::service('theme_handler')->listInfo(); $this->assertFalse(isset($theme_info['bartik']), 'Bartik theme uninstalled during import.'); // Verify that the action.settings configuration object was only deleted // once during the import process. $delete_called = \Drupal::state()->get('ConfigImportUITest.action.settings.delete', 0); $this->assertIdentical($delete_called, 1, "The action.settings configuration was deleted once during configuration import."); }
/** * {@inheritdoc} */ public function install(array $module_list, $enable_dependencies = TRUE) { $extension_config = \Drupal::configFactory()->getEditable('core.extension'); if ($enable_dependencies) { // Get all module data so we can find dependencies and sort. $module_data = system_rebuild_module_data(); $module_list = $module_list ? array_combine($module_list, $module_list) : array(); if ($missing_modules = array_diff_key($module_list, $module_data)) { // One or more of the given modules doesn't exist. throw new MissingDependencyException(sprintf('Unable to install modules %s due to missing modules %s.', implode(', ', $module_list), implode(', ', $missing_modules))); } // Only process currently uninstalled modules. $installed_modules = $extension_config->get('module') ?: array(); if (!($module_list = array_diff_key($module_list, $installed_modules))) { // Nothing to do. All modules already installed. return TRUE; } // Add dependencies to the list. The new modules will be processed as // the while loop continues. while (list($module) = each($module_list)) { foreach (array_keys($module_data[$module]->requires) as $dependency) { if (!isset($module_data[$dependency])) { // The dependency does not exist. throw new MissingDependencyException("Unable to install modules: module '{$module}' is missing its dependency module {$dependency}."); } // Skip already installed modules. if (!isset($module_list[$dependency]) && !isset($installed_modules[$dependency])) { $module_list[$dependency] = $dependency; } } } // Set the actual module weights. $module_list = array_map(function ($module) use($module_data) { return $module_data[$module]->sort; }, $module_list); // Sort the module list by their weights (reverse). arsort($module_list); $module_list = array_keys($module_list); } // Required for module installation checks. include_once $this->root . '/core/includes/install.inc'; /** @var \Drupal\Core\Config\ConfigInstaller $config_installer */ $config_installer = \Drupal::service('config.installer'); $sync_status = $config_installer->isSyncing(); if ($sync_status) { $source_storage = $config_installer->getSourceStorage(); } $modules_installed = array(); foreach ($module_list as $module) { $enabled = $extension_config->get("module.{$module}") !== NULL; if (!$enabled) { // Throw an exception if the module name is too long. if (strlen($module) > DRUPAL_EXTENSION_NAME_MAX_LENGTH) { throw new ExtensionNameLengthException("Module name '{$module}' is over the maximum allowed length of " . DRUPAL_EXTENSION_NAME_MAX_LENGTH . ' characters'); } // Check the validity of the default configuration. This will throw // exceptions if the configuration is not valid. $config_installer->checkConfigurationToInstall('module', $module); // Save this data without checking schema. This is a performance // improvement for module installation. $extension_config->set("module.{$module}", 0)->set('module', module_config_sort($extension_config->get('module')))->save(TRUE); // Prepare the new module list, sorted by weight, including filenames. // This list is used for both the ModuleHandler and DrupalKernel. It // needs to be kept in sync between both. A DrupalKernel reboot or // rebuild will automatically re-instantiate a new ModuleHandler that // uses the new module list of the kernel. However, DrupalKernel does // not cause any modules to be loaded. // Furthermore, the currently active (fixed) module list can be // different from the configured list of enabled modules. For all active // modules not contained in the configured enabled modules, we assume a // weight of 0. $current_module_filenames = $this->moduleHandler->getModuleList(); $current_modules = array_fill_keys(array_keys($current_module_filenames), 0); $current_modules = module_config_sort(array_merge($current_modules, $extension_config->get('module'))); $module_filenames = array(); foreach ($current_modules as $name => $weight) { if (isset($current_module_filenames[$name])) { $module_filenames[$name] = $current_module_filenames[$name]; } else { $module_path = drupal_get_path('module', $name); $pathname = "{$module_path}/{$name}.info.yml"; $filename = file_exists($module_path . "/{$name}.module") ? "{$name}.module" : NULL; $module_filenames[$name] = new Extension($this->root, 'module', $pathname, $filename); } } // Update the module handler in order to load the module's code. // This allows the module to participate in hooks and its existence to // be discovered by other modules. // The current ModuleHandler instance is obsolete with the kernel // rebuild below. $this->moduleHandler->setModuleList($module_filenames); $this->moduleHandler->load($module); module_load_install($module); // Clear the static cache of system_rebuild_module_data() to pick up the // new module, since it merges the installation status of modules into // its statically cached list. drupal_static_reset('system_rebuild_module_data'); // Update the kernel to include it. $this->updateKernel($module_filenames); // Allow modules to react prior to the installation of a module. $this->moduleHandler->invokeAll('module_preinstall', array($module)); // Now install the module's schema if necessary. drupal_install_schema($module); // Clear plugin manager caches. \Drupal::getContainer()->get('plugin.cache_clearer')->clearCachedDefinitions(); // Set the schema version to the number of the last update provided by // the module, or the minimum core schema version. $version = \Drupal::CORE_MINIMUM_SCHEMA_VERSION; $versions = drupal_get_schema_versions($module); if ($versions) { $version = max(max($versions), $version); } // Notify interested components that this module's entity types and // field storage definitions are new. For example, a SQL-based storage // handler can use this as an opportunity to create the necessary // database tables. // @todo Clean this up in https://www.drupal.org/node/2350111. $entity_manager = \Drupal::entityManager(); $update_manager = \Drupal::entityDefinitionUpdateManager(); foreach ($entity_manager->getDefinitions() as $entity_type) { if ($entity_type->getProvider() == $module) { $update_manager->installEntityType($entity_type); } elseif ($entity_type->isSubclassOf(FieldableEntityInterface::CLASS)) { // The module being installed may be adding new fields to existing // entity types. Field definitions for any entity type defined by // the module are handled in the if branch. foreach ($entity_manager->getFieldStorageDefinitions($entity_type->id()) as $storage_definition) { if ($storage_definition->getProvider() == $module) { // If the module being installed is also defining a storage key // for the entity type, the entity schema may not exist yet. It // will be created later in that case. try { $update_manager->installFieldStorageDefinition($storage_definition->getName(), $entity_type->id(), $module, $storage_definition); } catch (EntityStorageException $e) { watchdog_exception('system', $e, 'An error occurred while notifying the creation of the @name field storage definition: "!message" in %function (line %line of %file).', ['@name' => $storage_definition->getName()]); } } } } } // Install default configuration of the module. $config_installer = \Drupal::service('config.installer'); if ($sync_status) { $config_installer->setSyncing(TRUE)->setSourceStorage($source_storage); } \Drupal::service('config.installer')->installDefaultConfig('module', $module); // If the module has no current updates, but has some that were // previously removed, set the version to the value of // hook_update_last_removed(). if ($last_removed = $this->moduleHandler->invoke($module, 'update_last_removed')) { $version = max($version, $last_removed); } drupal_set_installed_schema_version($module, $version); // Ensure that all post_update functions are registered already. /** @var \Drupal\Core\Update\UpdateRegistry $post_update_registry */ $post_update_registry = \Drupal::service('update.post_update_registry'); $post_update_registry->registerInvokedUpdates($post_update_registry->getModuleUpdateFunctions($module)); // Record the fact that it was installed. $modules_installed[] = $module; // Drupal's stream wrappers needs to be re-registered in case a // module-provided stream wrapper is used later in the same request. In // particular, this happens when installing Drupal via Drush, as the // 'translations' stream wrapper is provided by Interface Translation // module and is later used to import translations. \Drupal::service('stream_wrapper_manager')->register(); // Update the theme registry to include it. drupal_theme_rebuild(); // Modules can alter theme info, so refresh theme data. // @todo ThemeHandler cannot be injected into ModuleHandler, since that // causes a circular service dependency. // @see https://www.drupal.org/node/2208429 \Drupal::service('theme_handler')->refreshInfo(); // In order to make uninstalling transactional if anything uses routes. \Drupal::getContainer()->set('router.route_provider.old', \Drupal::service('router.route_provider')); \Drupal::getContainer()->set('router.route_provider', \Drupal::service('router.route_provider.lazy_builder')); // Allow the module to perform install tasks. $this->moduleHandler->invoke($module, 'install'); // Record the fact that it was installed. \Drupal::logger('system')->info('%module module installed.', array('%module' => $module)); } } // If any modules were newly installed, invoke hook_modules_installed(). if (!empty($modules_installed)) { \Drupal::getContainer()->set('router.route_provider', \Drupal::service('router.route_provider.old')); if (!\Drupal::service('router.route_provider.lazy_builder')->hasRebuilt()) { // Rebuild routes after installing module. This is done here on top of // \Drupal\Core\Routing\RouteBuilder::destruct to not run into errors on // fastCGI which executes ::destruct() after the module installation // page was sent already. \Drupal::service('router.builder')->rebuild(); } $this->moduleHandler->invokeAll('modules_installed', array($modules_installed)); } return TRUE; }