Jump to content

Swap field language values


Can
 Share

Recommended Posts

Hola amigos

I hope the title is somehow proper.

We started our website german first and added the english version later on.

So it's domain.com/ and domain.com/en

Now we want to change this. But I don't want to redirect things or so.

I would love to swap the values of the multilang fields.

Changing the languages themselves worked quite good, just renamed them and changed the prefix for second language and changed url prefix on homepage too.

This one seems to be related and Soma already mentioned he could help with a little snippet as starting point

https://processwire.com/talk/topic/5062-switch-default-language-possible/?p=49531

I'm on PW 2.4.9 and using build in language support with multi language page names

Cheers, Can

Ah, that's what I already tried, but looks like nothing happens :/

$english = $languages->get(6892);
$german = $languages->get(6572);
foreach($pages->find("limit=100,start=1600,template=article|image|gallery-album|basic-page|blog|books|contact|feed|gallery|home|http404|search|tag|tags|sitemap|sitemap-xml|login|logout|profile|register|reset-pass|comment|comments|approve") as $p)
{
	$p->of(false);

	$detitle = '';
	$entitle = '';
	$desubtitle = '';
	$ensubtitle = '';
	$deheadline = '';
	$enheadline = '';
	$desummary = '';
	$ensummary = '';
	$debody = '';
	$enbody = '';
	$dehappygaiarelatives = '';
	$enhappygaiarelatives = '';
	$detweet = '';
	$entweet = '';
	$detweet_id = '';
	$entweet_id = '';
	$devideo = '';
	$envideo = '';

	if( $p->title && $p->getUnformatted('title')->getLanguageValue($english) ) $entitle = $p->getUnformatted('title')->getLanguageValue($english);
	if( $p->title && $p->getUnformatted('title')->getLanguageValue($german) ) $detitle = $p->getUnformatted('title')->getLanguageValue($german);

	if( $p->subtitle && $p->getUnformatted('subtitle')->getLanguageValue($english) ) $ensubtitle = $p->getUnformatted('subtitle')->getLanguageValue($english);
	if( $p->subtitle && $p->getUnformatted('subtitle')->getLanguageValue($german) ) $desubtitle = $p->getUnformatted('subtitle')->getLanguageValue($german);

	if( $p->headline && $p->getUnformatted('headline')->getLanguageValue($english) ) $enheadline = $p->getUnformatted('headline')->getLanguageValue($english);
	if( $p->headline && $p->getUnformatted('headline')->getLanguageValue($german) ) $deheadline = $p->getUnformatted('headline')->getLanguageValue($german);

	if( $p->summary && $p->getUnformatted('summary')->getLanguageValue($english) ) $ensummary = $p->getUnformatted('summary')->getLanguageValue($english);
	if( $p->summary && $p->getUnformatted('summary')->getLanguageValue($german) ) $desummary = $p->getUnformatted('summary')->getLanguageValue($german);

	if( $p->body && $p->getUnformatted('body')->getLanguageValue($english) ) $enbody = $p->getUnformatted('body')->getLanguageValue($english);
	if( $p->body && $p->getUnformatted('body')->getLanguageValue($german) ) $debody = $p->getUnformatted('body')->getLanguageValue($german);

	if( $p->happygaiarelatives && $p->getUnformatted('happygaiarelatives')->getLanguageValue($english) ) $enhappygaiarelatives = $p->getUnformatted('happygaiarelatives')->getLanguageValue($english);
	if( $p->happygaiarelatives && $p->getUnformatted('happygaiarelatives')->getLanguageValue($german) ) $dehappygaiarelatives = $p->getUnformatted('happygaiarelatives')->getLanguageValue($german);

	if( $p->tweet && $p->getUnformatted('tweet')->getLanguageValue($english) ) $entweet = $p->getUnformatted('tweet')->getLanguageValue($english);
	if( $p->tweet && $p->getUnformatted('tweet')->getLanguageValue($german) ) $detweet = $p->getUnformatted('tweet')->getLanguageValue($german);

	if( $p->tweet_id && $p->getUnformatted('tweet_id')->getLanguageValue($english) ) $entweet_id = $p->getUnformatted('tweet_id')->getLanguageValue($english);
	if( $p->tweet_id && $p->getUnformatted('tweet_id')->getLanguageValue($german) ) $detweet_id = $p->getUnformatted('tweet_id')->getLanguageValue($german);

	if( $p->video && $p->getUnformatted('video')->getLanguageValue($english) ) $envideo = $p->getUnformatted('video')->getLanguageValue($english);
	if( $p->video && $p->getUnformatted('video')->getLanguageValue($german) ) $devideo = $p->getUnformatted('video')->getLanguageValue($german);

	if( $p->image ) $deimage = $p->image;
	if( $p->image_en ) $enimage = $p->image_en;

	if(!empty($detitle)) $p->title->setLanguageValue($english, $detitle);
	if(!empty($entitle)) $p->title->setLanguageValue($german, $entitle);

	if(!empty($desubtitle)) $p->subtitle->setLanguageValue($english, $desubtitle);
	if(!empty($ensubtitle)) $p->subtitle->setLanguageValue($german, $ensubtitle);

	if(!empty($deheadline)) $p->headline->setLanguageValue($english, $deheadline);
	if(!empty($enheadline)) $p->headline->setLanguageValue($german, $enheadline);

	// if(!empty($desummary)) $p->summary->setLanguageValue($english, $desummary);
	// if(!empty($ensummary)) $p->summary->setLanguageValue($german, $ensummary);

	if(!empty($debody)) $p->body->setLanguageValue($english, $debody);
	if(!empty($enbody)) $p->body->setLanguageValue($german, $enbody);

	if(!empty($dehappygaiarelatives)) $p->happygaiarelatives->setLanguageValue($english, $dehappygaiarelatives);
	if(!empty($enhappygaiarelatives)) $p->happygaiarelatives->setLanguageValue($german, $enhappygaiarelatives);

	if(!empty($detweet)) $p->tweet->setLanguageValue($english, $detweet);
	if(!empty($entweet)) $p->tweet->setLanguageValue($german, $entweet);

	if(!empty($detweet_id)) $p->tweet_id->setLanguageValue($english, $detweet_id);
	if(!empty($entweet_id)) $p->tweet_id->setLanguageValue($german, $entweet_id);

	if(!empty($devideo)) $p->video->setLanguageValue($english, $devideo);
	if(!empty($envideo)) $p->video->setLanguageValue($german, $envideo);

	if(!empty($deimage)) $p->image_de = $deimage;
	if(!empty($enimage)) $p->image = $enimage;

	$p->save();
}

So i tried to store the values in variables if set so they're not overriding.

Hope someone can direct me the right way.

UPDATE

Looks like it's working now, I'm not really sure where the problem was but the code worked now for me. Except for the images, but maybe I can fix them or I'll do them manually as there are only a couple of language alternate images in use.

Edited by Can
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...