Bhupinder Bisht
Bhupinder Bisht

Reputation: 43

Pimcore While localization of a field move existing data to localized en(english) language

I have a existing project developed on pimcore 3. I am facing a challenge while localizing the existing fields of a class, as on moving a field into the localized field container means the loss of data from the field in all objects using this class. I don't want to loss that data and instead wanted that data to be moved to english language of that localizedfield container.

Let's say I have a class "test" with below fields:

Let's say now I want to localize below 3 fields for English , French and German.

But I don't want my existing data to be lost, instead I want the english language of field of that localization to be filled up with existing data.

Thanks in advance.

Upvotes: 3

Views: 572

Answers (1)

Igor Benko
Igor Benko

Reputation: 1276

Unfortunately this is not completely straightforward. In the most simple case you should write two simple scripts.

Follow this workflow:

  • duplicate the three fields, eg. NameTemp, DescTemp and CategoryTemp
  • create a script that copies the data from Name to NameTemp, from Desc to DescTemp and Category to CategoryTemp

After you copied do this:

  • move fields Name, Desc, Category into localized field - (your data is still left in NameTemp, DescTemp and CategoryTemp)
  • now create a new script which copies the data from NameTemp to Name, DescTemp to Desc and CategoryTemp to Category

Sample script (NOT TESTED):

// First run copyValueFromFieldsToTempFieldsAction(), 
// then move the fields into localized field,
// afterwards run copyValueFromTempFieldsToFieldsAction()
// in the end remove temp fields
public function copyValueFromFieldsToTempFieldsAction(){
  $this->disableViewAutoRender();

  $objectList = new \Pimcore\Model\YourObject\Listing();
  //$object_list->setObjectTypes(array(Object_Abstract::OBJECT_TYPE_VARIANT));

  /* @var $object Object_PrdktResetkeNaZalogi */
  foreach($objectList as $object){
      $object->setNameTemp($object->getName());
      $object->setDescTemp($object->getTemp());
      $object->setCategoryTemp($object->getCategory());

      $object->save();
  }
}

public function copyValueFromTempFieldsToFieldsAction(){
  $this->disableViewAutoRender();

  $objectList = new \Pimcore\Model\YourObject\Listing();
  //$object_list->setObjectTypes(array(Object_Abstract::OBJECT_TYPE_VARIANT));

  /* @var $object Object_PrdktResetkeNaZalogi */
  foreach($objectList as $object){
      $object->setTemp($object->getNameTemp());
      $object->setDesc($object->getTemp());
      $object->setCategory($object->getCategorTemp());

      $object->save();
  }
}

Upvotes: 3

Related Questions