3

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?

3

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.

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

<form method="post" accept-charset="utf-8">

    {{ getCsrfInput() }}
    <input type="hidden" name="action" value="profile/saveProfile">

    ...

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

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

    ...

    <button type="button" id="addAward">Add Award</button>

    ...
</form>

javascript

var awardBlockNum = 1;

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

        $(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.
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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