Jump to content

How to duplicate one language field to another


Pascal_761
 Share

Recommended Posts

Hello,

I need to duplicate every field that exists in one particular language to another language (fr-fr to fr-be), and after that some adjustments have to be made on fr-be pages.

Can someone give me any clues to achieve this with mysql queries (processwire database structure is new for me - mysql is not :) )

Thx

Link to comment
Share on other sites

Assuming that your languages have the names "fr" and "be", and that you want to do this for a field named "body", you could loop through all pages with that field and do this:

foreach($pages->find("body!=") as $p) {
    $page->of(false);
    $page->body_be = $page->body_fr;
    $page->save();
}

See here: https://processwire.com/api/multi-language-support/multi-language-fields/#language-alternate-field-values

And welcome to the forum!

Link to comment
Share on other sites

Hello and welcome!

I am not good at MySQL really. But you could achieve that with php script. The below code is an example, not tested yet. But you should get the idea.

<?php

// First get all the pages you want to manipulate
$list = $pages->find("template=example");

// The language from which you want the values
$langFrom = $languages->get('fr-fr');

// The language into which you want the values
$langTo = $languages->get('fr-be');

// List of all fields that should be copied
// if a page has it. Some fields does not have a
// multiple language entries. Like Images, Files, Checkboxes
// this will help us avoid errors
$validFields = array(
  'title',
  'body',
  'summary',
  'sidebar'
  );

// Loop through pages
foreach ($list as $p) {
  
  // Turn off outputFormatting
  $p->of(false);
  
  // then loop through each field
  foreach ($p->fields as $field) {

    // If the field is not one of those we want to copy then ignore
    if (!in_array($field->name, $validFields)) continue;

    // Get the value of the field for from language
    $fromValue = $p->getLanguageValue($langFrom, $field->name);

    // Here you can do any manipulations to your value 
    // if you want to alter it a little
    // ...
    // ...

    // Set the value of the field for to language
    $p->setLanguageValue($langTo, $fromValue);
  }

  $p->save();

  echo "$p->title is updated<br />";
}

Note: If there are too many pages on which you need to perform this, consider limiting the list and doing it with small batches. Like...

<?php

$limit = 100;
$page = 1;
$start = ($page - 1) * $limit;

$list = $pages->find("template=example, limit=$limit, start=$start");
// and the rest is like above
// ...
  • Like 1
Link to comment
Share on other sites

Thanks for all your responses, I was able to find a solution with mysql in the meantime.

I figured out that language fields have all the same name in the database : data1013 -> 'fr', data1017 -> 'be' in my case.

To find every table that have language fields i used this query :

SELECT DISTINCT TABLE_NAME as mytable
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE COLUMN_NAME IN ('data1013')
        AND TABLE_SCHEMA='processwire';

and on every table found i applied this query to duplicate the fields :

UPDATE <table_name> SET data1017 = data1013;

And of course i made a backup of my database before i made any update query :)

  • Like 1
Link to comment
Share on other sites

  • 4 years later...

Hey...

I've found this post because I had to accomplish a similar task.

@dadish thank you for sharing this code-example. If anybody stumbles up this post again I want to mention that there's a little error inside: Instead of

On 8/5/2015 at 3:32 PM, dadish said:

 


// Set the value of the field for to language     
$p->setLanguageValue($langTo, $fromValue);

you have to set the new language value like this:

//Set the value of the field for to language
$p->setLanguageValue($langTo, $field, $fromValue);

See https://processwire.com/api/ref/page/#api-setLanguageValue

Many greets!

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...