Answers.Body: <p>In case anyone is interested this is how I solved the problem:</p>
  
  <p>First load the matrix field:</p>
  
  <pre><code>$addressMatrix = craft()-&gt;fields-&gt;getFieldByHandle("addressMatrix");
  </code></pre>
  
  <p>Then get all of the blocks of the matrix field:</p>
  
  <pre><code>$addressMatrixBlocks = craft()-&gt;matrix-&gt;getBlockTypesByFieldId($addressMatrix-&gt;id);
  </code></pre>
  
  <p>Next iterate over the blocks looking for a specific handle:</p>
  
  <pre><code>foreach($addressMatrixBlocks as $key =&gt; $data){ 
  
      if($data-&gt;handle === "address"){ 
  
      $addressBlock = $data; 
      break; 
  
      }; 
  
  };
  </code></pre>
  
  <p>Now you have your block. To get the ID just do this:</p>
  
  <pre><code>echo $addressBlock-&gt;id;
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-08T12:46:08.040Z
Answers.LastActivityDate: 2015-04-08T12:46:08.040Z
Answers.UserDisplayName: Matt V
Answers.Body: <p>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.</p>
  
  <pre><code>{% for field in entry.getFieldLayout().getFields() %}
      {% if field.getField().handle == "myFieldHandle" %}
          {% for blockType in field.getField().getFieldType().getSettings().getBlockTypes() %}
              {{ blockType.id }}
          {% endfor %}
      {% endif %}
  {% endfor %}
  </code></pre>
  
  <p>The easiest would be to just hard-code the blockTypeId if that's an option.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-07T01:39:19.447Z
Answers.LastActivityDate: 2015-04-07T01:39:19.447Z
Answers.UserDisplayName: Douglas McDonald
Body: <p>I'm working on a plugin to save user profile data. I have a matrix field called <strong>addressMatrix</strong>, I've made address a matrix because users can save multiple addresses. Every address entry will have the following fields:</p>
  
  <ul>
  <li>streetAddress</li>
  <li>city</li>
  <li>stateProvince</li>
  <li>postalCode</li>
  <li>country</li>
  </ul>
  
  <p>NOTE: I did associate the field with users.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>Thank you for any help you can provide.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Here's the code I have thus far:</p>
  
  <pre><code>// Load the user
  $userModel = craft()-&gt;users-&gt;getUserById($profileDataModel-&gt;userId);
  
  // Load the matrix field
  $addressMatrix = craft()-&gt;fields-&gt;getFieldByHandle("addressMatrix");
  
  $block = new MatrixBlockModel();
  $block-&gt;fieldId    = $addressMatrix-&gt;id; // Matrix field's ID
  $block-&gt;ownerId    = $userModel-&gt;id; // ID of entry the block should be added to
  $block-&gt;typeId     = 7; // ID of block type
  
  $block-&gt;getContent()-&gt;setAttributes(array(
      'streetAddress' =&gt; '1523 Kansas Ave'
  ));
  
  $success = craft()-&gt;matrix-&gt;saveBlock($block);
  </code></pre>
  
  <p>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.</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-04-06T17:53:55.663Z
Comments.Text: Check out [this answer](http://craftcms.stackexchange.com/a/1414/784), which in the comments links to [nice code example](https://gist.github.com/bwlng/5c0bdeff62fabe247c87) illustrating how to add or update matrix blocks.
Comments.UserDisplayName: Douglas McDonald
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-04-06T18:53:14.910Z
Comments.Text: @ Douglas: Please see my updated statement. Thanks
Comments.UserDisplayName: Matt V
Tags: <plugin-development><matrix><plugins>
Title: Creating a matrix entry with a plugin
ThreadText: # Creating a matrix entry with a plugin
  
  (Asked by: username_0 on 2015-04-06)
  
  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.
  
  **Tags:** plugin-development, matrix, plugins
  
  > **Comments:**
  > **username_1**: Check out [this answer](http://craftcms.stackexchange.com/a/1414/784), which in the comments links to [nice code example](https://gist.github.com/bwlng/5c0bdeff62fabe247c87) illustrating how to add or update matrix blocks.  
  > **username_0**: @ username_1: Please see my updated statement. Thanks  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2015-04-08. Score: 3
  
  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; 
      break; 
  
      }; 
  
  };
  
  ```
  
  Now you have your block. To get the ID just do this:
  
  ```
  echo $addressBlock->id;
  
  ```
  
  
  ---
  
  ### Answer by username_1 on 2015-04-07. Score: 2
  
  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.
  
  
  ---
  
  