logger = $logger; $this->moduleHandler = $module_handler; $this->entityManager = $entity_manager; $this->configDiff = $config_diff; $this->configList = $config_list; $this->configUpdate = $config_update; } /** * {@inheritdoc} */ public function rebuild(): void { // Get a list of excluded config. $exclude_config = $this->getExcludedItems(); // Build a list of config to revert, without excluded config. $revert_config = array_diff($this->getDifferentItems('type', 'system.all'), $exclude_config); // Iterate through config items and revert them. foreach ($revert_config as $name) { // Get the config type and bail if simple configuration. // The lister gives NULL if simple configuration. $type = $this->configList->getTypeNameByConfigName($name); if ($type === NULL) { continue; } // Get the config short name. $shortname = $this->getConfigShortname($type, $name); // Perform the operation. $result = $this->configUpdate->revert($type, $shortname); // Log the result. if ($result) { $this->logger->notice($this->t('Reverted config: @config', ['@config' => $name])); } else { $this->logger->error($this->t('Failed to revert config: @config', ['@config' => $name])); } } } /** * Lists excluded config items. * * Lists config items that should be excluded from all automatic updates. * * @return array * An array of config item names. */ protected function getExcludedItems() { // Ask modules for config exclusions. $exclude_config = $this->moduleHandler->invokeAll('farm_update_exclude_config'); // Load farm_update.settings to get additional exclusions. $settings_exclude_config = \Drupal::config('farm_update.settings')->get('exclude_config'); if (!empty($settings_exclude_config)) { $exclude_config = array_merge($exclude_config, $settings_exclude_config); } // Always exclude this module's configuration. // This isn't strictly necessary because we don't provide default config // in config/install. But in the unlikely event that a custom module does // provide this config, and then it is somehow overridden by another means, // it would be reverted. So we exclude it here just to be extra safe. $exclude_config[] = 'farm_update.settings'; return $exclude_config; } /** * Lists differing config items. * * Lists config items that differ from the versions provided by your * installed modules, themes, or install profile. See config-diff to show * what the differences are. * * This method is copied directly from ConfigUpdateUiCliService. * * @param string $type * Run the report for: module, theme, profile, or "type" for config entity * type. * @param string $name * The machine name of the module, theme, etc. to report on. See * config-list-types to list types for config entities; you can also use * system.all for all types, or system.simple for simple config. * * @return array * An array of differing configuration items. * * @see \Drupal\config_update_ui\ConfigUpdateUiCliService::getDifferentItems() */ protected function getDifferentItems($type, $name) { [$activeList, $installList, $optionalList] = $this->configList->listConfig($type, $name); $addedItems = array_diff($activeList, $installList, $optionalList); $activeAndAddedItems = array_diff($activeList, $addedItems); $differentItems = []; foreach ($activeAndAddedItems as $name) { $active = $this->configUpdate->getFromActive('', $name); $extension = $this->configUpdate->getFromExtension('', $name); if (!$this->configDiff->same($active, $extension)) { $differentItems[] = $name; } } sort($differentItems); return $differentItems; } /** * Gets the config item shortname given the type and name. * * This method is copied directly from ConfigUpdateUiCliService. * * @param string $type * The type of the config item. * @param string $name * The name of the config item. * * @return string * The shortname for the configuration item. * * @see \Drupal\config_update_ui\ConfigUpdateUiCliService::getConfigShortname() */ protected function getConfigShortname($type, $name) { $shortname = $name; if ($type != 'system.simple') { $definition = $this->entityManager->getDefinition($type); $prefix = $definition->getConfigPrefix() . '.'; if (strpos($name, $prefix) === 0) { $shortname = substr($name, strlen($prefix)); } } return $shortname; } }