<p>You can save Matrix blocks directly, without having to re-save the entry.</p>

<h2>Craft 2</h2>

<p>Use <a href="https://docs.craftcms.com/api/v2/craft-matrixservice.html#method-saveblock" rel="noreferrer">MatrixService::saveBlock()</a>:</p>



<pre class="lang-php prettyprint-override"><code>$block = new MatrixBlockModel();
$block-&gt;fieldId = 5;
$block-&gt;ownerId = 100;
$block-&gt;ownerLocale = 'en_us';
$block-&gt;typeId = 2;
$block-&gt;sortOrder = 10;

$block-&gt;setContentFromPost(array(
    'fieldHandle' =&gt; 'value',
    // ...
));

$success = craft()-&gt;matrix-&gt;saveBlock($block);
</code></pre>

<h2>Craft 3</h2>

<p>Use <a href="https://docs.craftcms.com/api/v3/craft-services-elements.html#method-saveelement" rel="noreferrer">craft\services\Elements::saveElement()</a>:</p>

<pre class="lang-php prettyprint-override"><code>use Craft;
use craft\elements\MatrixBlock;

$block = new MatrixBlock();
$block-&gt;fieldId = 5;
$block-&gt;ownerId = 100;
$block-&gt;siteId = 1;
$block-&gt;typeId = 2;
$block-&gt;sortOrder = 10;

$block-&gt;setFieldValues([
    'fieldHandle' =&gt; 'value',
    // ...
]);

$success = Craft::$app-&gt;elements-&gt;saveElement($block);
</code></pre>

CC BY-SA 4.0
2018-11-20T21:49:44.640Z
@BrandonKelly how has this changed in Craft 3? I don't see a `MatrixBlockModel`.
cmal
CC BY-SA 4.0
2018-11-21T13:41:03.383Z
@cmal just updated my answer for Craft 3.
Brandon Kelly
CC BY-SA 3.0
2014-07-26T01:15:34.807Z
Thanks, that worked for me.

Here's a gist that shows how this can be used to create a new entry if there isn't one for the block to be added to yet if anyone else needs to do this: https://gist.github.com/bwlng/5c0bdeff62fabe247c87

Would resaving the entry after adding all the blocks update the search keywords?
Stephen Bowling
CC BY-SA 3.0
2014-07-27T15:03:21.650Z
@StephenBowling Yes that’s a possibility. Just updated the code example showing how you could re-save the entry's content after saving the new matrix block.
Brandon Kelly
CC BY-SA 4.0
2014-07-25T23:13:34.337Z
2018-11-21T20:40:46.887Z
2018-11-21T20:40:46.887Z
Brandon Kelly
<p>I'm building a plugin that adds blocks to a matrix in an entry from the front end. I got it up and running by following the answer in this post: </p>

<p><a href="https://craftcms.stackexchange.com/questions/1101/how-to-save-a-matrix-content-of-a-new-entry-in-my-plugin/1218#1218">How to save a matrix content of a new entry in my plugin?</a></p>

<p>How do I check if the matrix already has blocks when I fetch the entry? And then, once I know if the matrix already has blocks, how do I retain the existing data while saving new blocks via the plugin?</p>

<plugin-development>
How do I add a block to a matrix that already has content via a plugin?
# How do I add a block to a matrix that already has content via a plugin?

(Asked by: username_0 on 2014-07-25)

I'm building a plugin that adds blocks to a matrix in an entry from the front end. I got it up and running by following the answer in this post:

[How to save a matrix content of a new entry in my plugin?](https://craftcms.stackexchange.com/questions/1101/how-to-save-a-matrix-content-of-a-new-entry-in-my-plugin/1218#1218)

How do I check if the matrix already has blocks when I fetch the entry? And then, once I know if the matrix already has blocks, how do I retain the existing data while saving new blocks via the plugin?

**Tags:** plugin-development

---

## 1 answers

### Answer by username_1 on 2014-07-25. Score: 13 | Accepted answer

You can save Matrix blocks directly, without having to re-save the entry.
Craft 2
Use MatrixService::saveBlock():
$block = new MatrixBlockModel();
$block->fieldId = 5;
$block->ownerId = 100;
$block->ownerLocale = 'en_us';
$block->typeId = 2;
$block->sortOrder = 10;

$block->setContentFromPost(array(
    'fieldHandle' => 'value',
    // ...
));

$success = craft()->matrix->saveBlock($block);

Craft 3
Use craft\services\Elements::saveElement():
use Craft;
use craft\elements\MatrixBlock;

$block = new MatrixBlock();
$block->fieldId = 5;
$block->ownerId = 100;
$block->siteId = 1;
$block->typeId = 2;
$block->sortOrder = 10;

$block->setFieldValues([
    'fieldHandle' => 'value',
    // ...
]);

$success = Craft::$app->elements->saveElement($block);

> **Comments:**
> **username_0**: Thanks, that worked for me.  Here's a gist that shows how this can be used to create a new entry if there isn't one for the block to be added to yet if anyone else needs to do this: https://gist.github.com/bwlng/5c0bdeff62fabe247c87  Would resaving the entry after adding all the blocks update the search keywords?  
> **username_1**: @StephenBowling Yes that’s a possibility. Just updated the code example showing how you could re-save the entry's content after saving the new matrix block.  
> **username_2**: @BrandonKelly how has this changed in Craft 3? I don't see a `MatrixBlockModel`.  
> **username_1**: @cmal just updated my answer for Craft 3.  

---

