I'm working on a plugin to save user profile data. I have a matrix field called addressMatrix, I've made address a matrix because users can save multiple addresses. Every address entry will have the following fields:

  • streetAddress
  • city
  • stateProvince
  • postalCode
  • country

NOTE: I did associate the field with users.

All I want to do is to be able to save a new address or update an existing address for each user. I have no idea where to begin.

I've looked at 1 or 2 Stack Exchange posts on the matter but it does not look like anyone else has raised this question. Sorry in advance... I have no code to post as I don't even have a solid starting point.

Thank you for any help you can provide.


Here's the code I have thus far:

// Load the user
$userModel = craft()->users->getUserById($profileDataModel->userId);

// Load the matrix field
$addressMatrix = craft()->fields->getFieldByHandle("addressMatrix");

$block = new MatrixBlockModel();
$block->fieldId    = $addressMatrix->id; // Matrix field's ID
$block->ownerId    = $userModel->id; // ID of entry the block should be added to
$block->typeId     = 7; // ID of block type

    'streetAddress' => '1523 Kansas Ave'

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

The above does work, but what I'm current stumped on is how to get the type ID programmatically. I should be able to get a block ID by the block handle. Looking through the class reference I don't see it.

  • Check out this answer, which in the comments links to nice code example illustrating how to add or update matrix blocks. Commented Apr 6, 2015 at 17:53
  • @ Douglas: Please see my updated statement. Thanks
    – Matt V
    Commented Apr 6, 2015 at 18:53

2 Answers 2


In case anyone is interested this is how I solved the problem:

First load the matrix field:

$addressMatrix = craft()->fields->getFieldByHandle("addressMatrix");

Then get all of the blocks of the matrix field:

$addressMatrixBlocks = craft()->matrix->getBlockTypesByFieldId($addressMatrix->id);

Next iterate over the blocks looking for a specific handle:

foreach($addressMatrixBlocks as $key => $data){ 

    if($data->handle === "address"){ 

    $addressBlock = $data; 



Now you have your block. To get the ID just do this:

echo $addressBlock->id;

Getting the block type id is not the easiest, as there can be more than one block and you don't necessarily have a block already defined in which to iterate over. In twig I was able to get to the blockType id by digging into the field layout using the following, which you should be able to adapt to php in your plugin. Not sure if there is an easier way.

{% for field in entry.getFieldLayout().getFields() %}
    {% if field.getField().handle == "myFieldHandle" %}
        {% for blockType in field.getField().getFieldType().getSettings().getBlockTypes() %}
            {{ blockType.id }}
        {% endfor %}
    {% endif %}
{% endfor %}

The easiest would be to just hard-code the blockTypeId if that's an option.

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.