<p>Finally the solution is found.
In Channel Setting is: <code>Save entries to all sites enabled for this section</code>.
In Neo Field Settings: <code>Only save blocks to the site the were created in</code>.</p>
<pre><code>$entry = new Entry();
$entry-&gt;setFieldValues($sampleData);
Craft::$app-&gt;getElements()-&gt;saveElement($entry);

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

CC BY-SA 4.0
2023-03-20T10:39:31.170Z
2023-03-20T10:39:31.170Z
Alex Kuzmin
<p>During the sample content migration I save programmatically the matrix/neo field value in entry using <code>saveElement</code> method. In field settings is set &quot;Only save blocks to the site they were created in&quot;. In the setting of each field block is set under &quot;Entry condition&quot; - site &gt; is one of &gt; correct locale.</p>
<p>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.</p>
<p>I've also tried to save an entry separately for each locale:</p>
<pre><code>$entry = new Entry();
$entry-&gt;setFieldValues($sampleData);
Craft::$app-&gt;getElements()-&gt;saveElement($entry);

$entry = Entry::find()-&gt;section('...')-&gt;slug('...')-&gt;one(); // here previous entry is found
$entry-&gt;siteId = 2;
$entry-&gt;setFieldValues($anotherSampleData);
Craft::$app-&gt;getElements()-&gt;saveElement($entry, true, false);
</code></pre>
<p>But it throughs an error <code>duplicate key value violates unique constraint</code>.</p>
<p>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.</p>

<localization><migration><plugin-neo>
Save matrix/neo field value programmatically only for one site/locale
# Save matrix/neo field value programmatically only for one site/locale

(Asked by: username_0 on 2023-03-17)

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.

**Tags:** localization, migration, plugin-neo

---

## 1 answers

### Answer by username_0 on 2023-03-20. Score: 1 | Accepted answer

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);


---

