my_module.install
<?php
/**
* Reset all search_api.index configs to their values from config/sync.
*/
function myzak_customs_post_update_10004() {
// Resolve the sync config directory.
$sync_path = \Drupal\Core\Site\Settings::get('config_sync_directory');
// Fallback: derive from DRUPAL_ROOT.
if (empty($sync_path)) {
$sync_path = DRUPAL_ROOT . '/../config/sync';
}
$sync_path = rtrim($sync_path, '/');
$messages = [];
// Find all search_api.index yml files.
$files = glob($sync_path . '/search_api.index.*.yml');
if (empty($files)) {
return "No search_api.index yml files found in: {$sync_path}";
}
foreach ($files as $file) {
$yaml_content = file_get_contents($file);
if ($yaml_content === FALSE) {
$messages[] = "Could not read file: {$file}";
continue;
}
// Parse YAML.
$config_data = \Symfony\Component\Yaml\Yaml::parse($yaml_content);
if (empty($config_data)) {
$messages[] = "Skipped " . basename($file) . ": empty or invalid YAML.";
continue;
}
$config_name = basename($file, '.yml');
// Load the active config.
$active_config = \Drupal::configFactory()->getEditable($config_name);
if ($active_config->isNew()) {
// Config doesn't exist in active config yet, create it fresh.
$active_config->setData($config_data)->save();
$messages[] = "Created {$config_name}: did not exist in active config.";
continue;
}
// Overwrite the entire config with the sync values.
$active_config->setData($config_data)->save();
$messages[] = "Reset {$config_name}: active config overwritten with sync values.";
}
// Invalidate caches for search_api so changes take effect.
\Drupal::service('cache.config')->invalidateAll();
// Log results.
\Drupal::logger('myzak_customs')->notice(implode("\n", $messages));
return implode("\n", $messages);
}
15.05.2026 | Dominik Wille