Skip to content

Instantly share code, notes, and snippets.

@gebeer
Last active November 14, 2015 07:31
Show Gist options
  • Save gebeer/9d6c969f5aae3aacf8f5 to your computer and use it in GitHub Desktop.
Save gebeer/9d6c969f5aae3aacf8f5 to your computer and use it in GitHub Desktop.
PHP function for swapping languages when changing the default language in ProcessWire
<?php
function swapLanguages($pages, $lang) {
$startTime = microtime(true);
$langDefault = "default";
$log = ["languages" => "Swap field values for language {$langDefault} with {$lang}", "pages" =>[]];
$languages = wire("languages");
// get languages
$l1 = $languages->get($langDefault);
$l2 = $languages->get($lang);
foreach ($pages as $p) {
$log["pages"][$p->id] = "";
$fieldlog = " name";
$p->of(false);
// swap page names
$nameDefault = $p->localName($l1);
$name = $p->localName($l2);
if($nameDefault != $name && !empty($name)) {
$p->set("name",$name);
$p->set("status$l2",1);
$p->set("name$l2",$nameDefault);
} elseif($nameDefault != $name && empty($name)) {
$p->set("status$l2",1);
$p->set("name$l2",$nameDefault);
}
// iterate through all fields and swap multi language field values
foreach ($p->template->fieldgroup as $field) {
if($field->type instanceof FieldtypePageTitleLanguage || $field->type instanceof FieldtypeTextareaLanguage || $field->type instanceof TextLanguage) {
$fieldlog .= " {$field->name}";
// var_dump($p->$field); // needed this for some pages otherwise they threw an error
// $p->of(false); // needed this for some pages otherwise they threw an error
$langDefault = $p->$field->getLanguageValue($l1);
$lang = $p->$field->getLanguageValue($l2);
if( $langDefault != $lang && !empty($lang) && !empty($langDefault) ) {
$p->$field->setLanguageValue($l1, $lang);
$p->$field->setLanguageValue($l2, $langDefault);
} elseif( $langDefault != $lang && empty($lang) ) {
$p->$field->setLanguageValue($l2, $langDefault);
} elseif( $langDefault != $lang && empty($langDefault) ) { // only needed this for pages that got created through API and had no default lang value set
$p->$field->setLanguageValue($l1, $lang);
}
}
}
try {
$p->save();
$log["pages"][$p->id] = "Swapped values in fields: {$fieldlog}";
} catch (Exception $e) {
$log["pages"][$p->id] = 'Error: ' . $e->getMessage();
}
$p->of(true);
}
// write logfile
$logfile = fopen(wire("config")->paths->logs . "swaplanguage-log.txt", "w") or die("Unable to open file!");
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($log));
foreach($iterator as $key => $value) {
fwrite($logfile, "$key => $value\n");
}
fclose($logfile);
$time_elapsed = microtime(true) - $startTime;
echo "Finished after {$time_elapsed} seconds";
}
/* Usage:
// swapLanguages($pages->find("template=basic-page, parent=1, include=all"), "de");
// argument 1: pages array
// argument 2: language name that you want to swap with the default language
// A log file will be written to site/assets/logs/swaplanguage-log.txt.
// It contains page IDs and fields that were swapped for each page
*/
/*
// After executing this function for all required pages, following steps are needed:
// 1. load core translation files for the language that is now your default (e.g. german translation files) to Setup->languages->default
// 2. change titles of the default Language to your language (e.g. Deutsch) and the language that got swapped (e.g. English)
// 3. if default language was English before swapping: delete core translation files for the language that you swapped
// 4. if default language was not English before swapping: load the core translation files for that language
// 4. in settings for home page, change the URL for your default language and the language that you have swapped.
// Example:
// Before swapping: default was en, swap language was de
// After swapping: default is now de, swapped language is now en
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment