Commit 2e8dff89 authored by Christopher Gervais's avatar Christopher Gervais
Browse files

Issue #3175060: Refactor deletion of multiple enforced configs.

parent e54e4ecc
Loading
Loading
Loading
Loading
+28 −0
Changes for src/EnforcedConfigCollection.php: 28 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -69,4 +69,32 @@ class EnforcedConfigCollection extends ReadOnlyEnforcedConfigCollection {
    }
  }

  /**
   * @param $enforced_configs_to_delete array of enforced config settings that need to deleted.
   */
  public function deleteEnforcedConfigs(array $enforced_configs_to_delete) {
    foreach ($this->getTargetModuleCollection()->getTargetModules() as $target_module) {
      $registry = $target_module->getRegistry();

      // Only delete enforced configs for this target module.
      $module_enforced_configs_to_delete = [];
      foreach ($enforced_configs_to_delete as $config_name => $settings) {
        // We skip any enforced configs destined for other target modules, as
        // they'll be processed on a subsequent iteration.
	if ($settings['target_module'] != $target_module->getMachineName()) continue;

        $module_enforced_configs_to_delete[$config_name] = $settings;

        // Drain the input array.
        unset($enforced_configs_to_delete[$config_name]);
      }
      $registry->deleteEnforcedConfigs($module_enforced_configs_to_delete);
    }

    // If we have any entries left in the input array, something's gone wrong.
    if (count($enforced_configs_to_delete)) {
      // @TODO: Log this?
    }
  }

}
+20 −0
Changes for src/EnforcedConfigRegistry.php: 20 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -102,6 +102,26 @@ class EnforcedConfigRegistry extends ReadOnlyEnforcedConfigRegistry {

  }

  /**
   * @param $enforced_configs_to_delete array of enforced config settings to delete.
   */
  public function deleteEnforcedConfigs(array $enforced_configs_to_delete) {
    if (empty($enforced_configs_to_delete)) return;

    $registry_config = $this->configFactory->getEditable($this->getConfigName());
    foreach ($enforced_configs_to_delete as $config_name => $settings) {
      // Remove the enforced config settings.
      $registry_config->clear(self::encode($config_name));
    }

    // @TODO: Also delete the config files from disk?

    // Save the updated registry config.
    $registry_config->save();
    // @TODO: Log this?

  }

  /**
   * Delete this registry of enforced configs.
   */
+17 −6
Changes for src/Form/EnforcedConfigs.php: 17 added lines, 6 removed lines.
Original line number Diff line number Diff line
@@ -92,15 +92,26 @@ class EnforcedConfigs extends FormBase {
   * Delete selected enforced config settings.
   */
  protected function deleteEnforcedConfigs() {
    $this->enforcedConfig = new EnforcedConfig();
    $enforced_configs = (new EnforcedConfigCollection())->getEnforcedConfigs();
    $enforced_configs_to_delete = $this->getEnforcedConfigsToDelete();
    (new EnforcedConfigCollection())->deleteEnforcedConfigs($enforced_configs_to_delete);

    $this->messenger()->addMessage(
      $this->t('Deleted enforcement settings for: %config_list', [
        '%config_list' => $this->renderHtmlList(array_keys($enforced_configs_to_delete)),
      ])
    );
  }

  /**
   * Return an array of enforced configs to delete.
   */
  protected function getEnforcedConfigsToDelete() {
    $enforced_configs = (new EnforcedConfigCollection())->getEnforcedConfigs();
    $enforced_configs_to_delete = [];
    foreach ($this->getSelectedConfigs() as $config_name) {
      $this->enforcedConfig
        ->setConfigName($config_name)
        ->setTargetModule($enforced_configs[$config_name]['target_module'])
        ->clear();
      $enforced_configs_to_delete[$config_name] = $enforced_configs[$config_name];
    }
    return $enforced_configs_to_delete;
  }

  /**