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.
UPDATE
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
$block->getContent()->setAttributes(array(
'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.