Add to Structure in specific language

Hi there,

I use the (popular) addToStructure Function which always works great! But now I have the problem that I want to add it only to one specific language. This seems not to work:

addToStructure(page('sample')->translation('de'), 'history',[
      'history_1' => $sample
]);

Any idea how to manage? Base code:

function addToStructure($page, $field, $data = array()){
      $fieldData = page($page)->$field()->yaml();
      $fieldData[] = $data;
      $fieldData = yaml::encode($fieldData);
      try {
        page($page)->update(array($field => $fieldData));
        //return true;
      } catch(Exception $e) {
        return $e->getMessage();
      }
    }

According to document:

 page('mypage')->update([
    'title'        => 'A new title',
    'text'         => 'Some text',
    'history'      => yaml::encode($fieldData);
  ], 'de');

That was also my idea, but for some reasons this deletes the field in the content file completely …

function addToStructure($page, $field, $data = array()){
      $fieldData = page($page)->$field()->yaml();
      $fieldData[] = $data;
      $fieldData = yaml::encode($fieldData);
      try {
        page($page)->update(array($field => $fieldData),'de');
      } catch(Exception $e) {
        return $e->getMessage();
      }
    }

I tested following codes, works for me, can you check it?

$page = page('sample');

$fieldData = $page->content('de')->history()->yaml();
$fieldData[] = [
    'history_1' => 'Sample'
];

$page->update([
    'history'   => Yaml::encode($fieldData)
], 'de');

No sorry, also not working! Everytime I do this request at a english page, all field datas are deleted … Do I understood this wrong? I wanted to put in all data that are collected only to the german file. Full code:

$kirby = kirby();
$kirby->impersonate('kirby');

function addToStructure($page, $field, $data = array()){
  $fieldData = page($page)->$field()->yaml();
  $fieldData[] = $data;
  $fieldData = yaml::encode($fieldData);
  try {
    page($page)->update(array($field => $fieldData),'de');
  } catch(Exception $e) {
    return $e->getMessage();
  }
}
 
addToStructure(page('conversions'), 'nachrichtenverlauf',[
  'nachrichtenverlauf_art' => 'Preisanfrage',
  'nachrichtenverlauf_fahrzeug' => $preisliste,
  'nachrichtenverlauf_datum' => $datum,
  'nachrichtenverlauf_uhrzeit' => $uhrzeit,
  'nachrichtenverlauf_email' => $email,
  'nachrichtenverlauf_sprache' => $sprache
]);

$kirby->impersonate();

What is your default language? What’s your Kirby version?

Default is “de”, Kirby is 3.2.0

Hm, I tested with a 3.2.1 Starterkit and cannot reproduce your issue. Maybe update the latest version (3.2.2) first, to make sure were are not dealing with a bug that has been fixed since.

Anyway, in my test, nothing is deleted and the German version is updated with the new content as expected.

Where did you put your code? Aare you using any plugins that might interfere?

(On a side note it would be great if you could choose the question category that corresponds to your Kirby version when creating a new topic here in the forum (indicating the exact version is alway a plus). Saves a lot of time for everyone if we don’t first have. to check the basics. Thank you.)

Hi Texnixe,

thanks a lot. The bug was fixed in a newer release. Updated to 3.2.2 and it’s working now :slight_smile: sorry about the forgotten question category, I will keep in mind