1

During the sample content migration I save programmatically the matrix/neo field value in entry using saveElement method. In field settings is set "Only save blocks to the site they were created in". In the setting of each field block is set under "Entry condition" - site > is one of > correct locale.

So I expect that the field block value is saved only for locale that is set in the settings of the block. It shows/hides the blocks correctly in Control Panel for each locale, but it saves the field block values for all locales, when I save the field value programmatically. Then, in twig templates I have loops for matrix/neo blocks, where I dynamically render all the available blocks on the current page, based on the handle of the block. Here I expect that some blocks are not available on some locales, but they are there.

I've also tried to save an entry separately for each locale:

$entry = new Entry();
$entry->setFieldValues($sampleData);
Craft::$app->getElements()->saveElement($entry);

$entry = Entry::find()->section('...')->slug('...')->one(); // here previous entry is found
$entry->siteId = 2;
$entry->setFieldValues($anotherSampleData);
Craft::$app->getElements()->saveElement($entry, true, false);

But it throughs an error duplicate key value violates unique constraint.

So maybe I'm doing something wrong? How would you separate different fields/neo field blocks for different locales, and how would you automatically fill the sample content for different locales? Thank you.

1 Answer 1

1

Finally the solution is found. In Channel Setting is: Save entries to all sites enabled for this section. In Neo Field Settings: Only save blocks to the site the were created in.

$entry = new Entry();
$entry->setFieldValues($sampleData);
Craft::$app->getElements()->saveElement($entry);

// here find previous entry and filter only for needed siteId, add different field values and save the entry again
$entry = Entry::find()->section('...')->slug('...')->siteId(2)->one(); 
$entry->setFieldValues($anotherSampleData);
Craft::$app->getElements()->saveElement($entry);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.