<p>There are a few threads on this that I found helpful. I think they are all worth looking at and reading through as my solution may vary based on your needs. </p>

<ul>
<li><p><a href="https://craftcms.stackexchange.com/questions/10371/save-matrix-block-to-new-entry-in-plugin">Save matrix block to new entry in plugin</a></p></li>
<li><p><a href="https://craftcms.stackexchange.com/questions/1101/how-to-save-a-matrix-content-of-a-new-entry-in-my-plugin">How to save a matrix content of a new entry in my plugin?</a></p></li>
<li><p><a href="https://gist.github.com/brandonkelly/8149062" rel="nofollow noreferrer">Saving new Matrix data</a></p></li>
</ul>

<p>Here is the complete solution that ended up working. If there are improvements, please let me know so I can update the thread accordingly!</p>

<p><strong>twig</strong></p>

<pre><code>&lt;form method="post" accept-charset="utf-8"&gt;

    {{ getCsrfInput() }}
    &lt;input type="hidden" name="action" value="profile/saveProfile"&gt;

    ...

    {% if profile.awardsHonors|length %}
        &lt;div id="awards"&gt;
            {% for block in profile.awardsHonors %}
                &lt;div class="matrixblock award"&gt;
                    &lt;div class="form-group"&gt;
                        &lt;input type="hidden" name="fields[awardsHonors][{{ block.id }}][type]" value="award"&gt;
                        &lt;input type="hidden" name="fields[awardsHonors][{{ block.id }}][enabled]" value="1"&gt;

                        &lt;input type="text" name="fields[awardsHonors][{{ block.id }}][fields][award]" class="form-control" value="{{ block.award }}"&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
                {% set blockId = block.id %}
            {% endfor %}
        &lt;/div&gt;
    {% else %}
        &lt;div id="awards"&gt;&lt;/div&gt;
    {% endif %}

    ...

    &lt;button type="button" id="addAward"&gt;Add Award&lt;/button&gt;

    ...
&lt;/form&gt;
</code></pre>

<p><strong>javascript</strong></p>

<pre><code>var awardBlockNum = 1;

$('#addAward').click(function (event) {
    var markup = '&lt;div class="matrixblock award"&gt;' +
        '&lt;div class="form-group"&gt;' +
        '&lt;input type="hidden" name="fields[awardsHonors][new' + awardBlockNum + '][type]" value="award"&gt;' +
        '&lt;input type="hidden" name="fields[awardsHonors][new' + awardBlockNum + '][enabled]" value="1"&gt;' +
        '&lt;input type="text" name="fields[awardsHonors][new' + awardBlockNum + '][fields][award]" class="form-control"&gt;' +
        '&lt;/div&gt;' +
        '&lt;/div&gt;';

        $(markup).appendTo($('#awards'));

        awardBlockNum++;
    });
</code></pre>

<p><strong>MyPluginController</strong></p>

<pre><code>public function actionSaveProfile()
{
    $criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
    $criteria-&gt;section = 'mySectionHandle';
    $criteria-&gt;limit = 1;
    $criteria-&gt;status = null;
    $criteria-&gt;authorId = craft()-&gt;request-&gt;getPost('userId');

    $entries = $criteria-&gt;find();

    if ($entries) {
        foreach ($criteria as $entry) {
            $awardMatrixData = array();

            /**
             * New matrix fields are prefixed with 'new1'.
             * To compensate we need to create a new increment variable to correctly store the matrix number
             * while looping the correct amount of times.
             */

            foreach (craft()-&gt;request-&gt;getPost('fields.awardsHonors') as $key =&gt; $value) {
                $i = 1;

                if ($key == 'new' . $i) { // Check for a new award.
                    $awardMatrixData['new' . $i] = array(
                        'type' =&gt; 'award',
                        'enabled' =&gt; true,
                        'fields' =&gt; array(
                            'award' =&gt; craft()-&gt;request-&gt;getPost('fields.awardsHonors.new' . $i . '.fields.award')
                        )
                    );
                } else { // Update existing awards.
                    $awardMatrixData[$key] = array(
                        'type' =&gt; 'award',
                        'enabled' =&gt; true,
                        'fields' =&gt; array(
                            'award' =&gt; craft()-&gt;request-&gt;getPost('fields.awardsHonors.' . $key . '.fields.award')
                        )
                    );
                }

                $i++;
            }

            $entry-&gt;setContentFromPost(array(
                ...
                ...
                'awardsHonors' =&gt; $matrixData
            ));

            if (craft()-&gt;entries-&gt;saveEntry($entry)) {
                // Profile updated!;
            } else {
                // Could not save entry (profile).
            }
        }
    } else {
        // No entry found that matches criteria.
    }
}
</code></pre>

CC BY-SA 3.0
2016-03-30T12:43:50.163Z
2016-04-01T18:02:47.927Z
2017-04-13T12:50:59.930Z
Damon
<p>I would like to have a front-end profile form on my website that contains a matrix field. The matrix field will allow the user to add an infinite number of awards they have earned. How can I do this?</p>

<plugin-development><matrix>
How can I add a dynamic number of rows to a matrix via plugin?
# How can I add a dynamic number of rows to a matrix via plugin?

(Asked by: username_0 on 2016-03-30)

I would like to have a front-end profile form on my website that contains a matrix field. The matrix field will allow the user to add an infinite number of awards they have earned. How can I do this?

**Tags:** plugin-development, matrix

---

## 1 answers

### Answer by username_0 on 2016-03-30. Score: 3 | Accepted answer

There are a few threads on this that I found helpful. I think they are all worth looking at and reading through as my solution may vary based on your needs. 

Save matrix block to new entry in plugin
How to save a matrix content of a new entry in my plugin?
Saving new Matrix data

Here is the complete solution that ended up working. If there are improvements, please let me know so I can update the thread accordingly!
twig


    {{ getCsrfInput() }}
    

    ...

    {% if profile.awardsHonors|length %}
        
            {% for block in profile.awardsHonors %}
                
                    
                        
                        

                        
                    
                
                {% set blockId = block.id %}
            {% endfor %}
        
    {% else %}
        
    {% endif %}

    ...

    Add Award

    ...


javascript
var awardBlockNum = 1;

$('#addAward').click(function (event) {
    var markup = '' +
        '' +
        '' +
        '' +
        '' +
        '' +
        '';

        $(markup).appendTo($('#awards'));

        awardBlockNum++;
    });

MyPluginController
public function actionSaveProfile()
{
    $criteria = craft()->elements->getCriteria(ElementType::Entry);
    $criteria->section = 'mySectionHandle';
    $criteria->limit = 1;
    $criteria->status = null;
    $criteria->authorId = craft()->request->getPost('userId');

    $entries = $criteria->find();

    if ($entries) {
        foreach ($criteria as $entry) {
            $awardMatrixData = array();

            /**
             * New matrix fields are prefixed with 'new1'.
             * To compensate we need to create a new increment variable to correctly store the matrix number
             * while looping the correct amount of times.
             */

            foreach (craft()->request->getPost('fields.awardsHonors') as $key => $value) {
                $i = 1;

                if ($key == 'new' . $i) { // Check for a new award.
                    $awardMatrixData['new' . $i] = array(
                        'type' => 'award',
                        'enabled' => true,
                        'fields' => array(
                            'award' => craft()->request->getPost('fields.awardsHonors.new' . $i . '.fields.award')
                        )
                    );
                } else { // Update existing awards.
                    $awardMatrixData[$key] = array(
                        'type' => 'award',
                        'enabled' => true,
                        'fields' => array(
                            'award' => craft()->request->getPost('fields.awardsHonors.' . $key . '.fields.award')
                        )
                    );
                }

                $i++;
            }

            $entry->setContentFromPost(array(
                ...
                ...
                'awardsHonors' => $matrixData
            ));

            if (craft()->entries->saveEntry($entry)) {
                // Profile updated!;
            } else {
                // Could not save entry (profile).
            }
        }
    } else {
        // No entry found that matches criteria.
    }
}


---

