Answers.Body: <p>Matrix blocks are elements just like entries and assets, so this is just a matter of setting the <code>elementType</code> to <code>'MatrixBlock'</code>, and adjusting your <code>criteria</code> to fetch the block you’re after.</p>
  
  <p>The simplest approach would be to pass the Matrix block ID in the URL, and apply it to the <code>id</code> criteria parameter.</p>
  
  <pre class="lang-php prettyprint-override"><code>'endpoints' =&gt; [
      'matrix-blocks/&lt;blockId:\d+&gt;' =&gt; function($blockId) {
          return [
              'elementType' =&gt; 'MatrixBlock',
              'criteria' =&gt; [
                  'id' =&gt; $blockId
              ],
              'first' =&gt; true, // Just return one Matrix block
              'transformer' =&gt; function(MatrixBlock $block) {
                  $assetsInfo = [];
  
                  foreach ($block-&gt;myAssetsfield as $asset) {
                      $assetsInfo[] = [
                          'url' =&gt; $asset-&gt;url,
                          'title' =&gt; $asset-&gt;title,
                          // ...
                      ];
                  }
  
                  return [
                      'id' =&gt; $block-&gt;id,
                      'assets' =&gt; $AssetsInfo,
                      // ...
                  ];
              },
          ];
      }
  ]
  </code></pre>
  
  <p>You can also use the following criteria parameters to narrow down the search:</p>
  
  <ul>
  <li><code>fieldId</code> (the Matrix field ID)</li>
  <li><code>ownerId</code> (the owner element ID, e.g. your entry’s ID)</li>
  <li><code>ownerLocale</code> (the locale of the owner element that the block should have been saved to)</li>
  <li><code>type</code> (the block type handle)</li>
  </ul>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-20T21:29:24.060Z
Answers.Text: Thank you! Seems easier than I thought. One little question. My matrix block has an assets field. Can I get them directly from $block or would I have to make an additional query?
Answers.UserDisplayName: exophunk
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-20T21:33:18.333Z
Answers.Text: @RobertKrieg Just updated my answer with an example of adding assets info to the response.
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-20T21:20:34.753Z
Answers.LastActivityDate: 2015-10-20T21:32:58.617Z
Answers.LastEditDate: 2015-10-20T21:32:58.617Z
Answers.UserDisplayName: Brandon Kelly
Body: <p>I have a question regarding the ElementAPI:</p>
  
  <p>I am sucessfully using the ElementAPI to fetch assets related to an entry. Now I have a matrix field where a user can add multiple galleries (multiple block of selected assets) and i want to retrieve the images. But I can't figure out how to use it for nested matrix blocks...</p>
  
  <p>This is how I fetch the data with ElementAPI from non-matrix assets fields (<code>elementapi.php</code> config)</p>
  
  <pre><code>'endpoints' =&gt; [
      'api/v1/installation-views/&lt;sourceEntryId:\d+&gt;/&lt;fieldHandle:[A-Za-z]+&gt;' =&gt; function($sourceEntryId, $fieldHandle) {
          return [
              'elementType' =&gt; 'Asset',
              'criteria' =&gt; [
                  'relatedTo' =&gt; [
                      'sourceElement' =&gt; $sourceEntryId,
                      'field' =&gt; $fieldHandle
                  ],
                  'kind' =&gt; 'image'
              ],
              'transformer' =&gt; function(AssetFileModel $asset) {
                  return [
                      'id' =&gt; $asset-&gt;id,
                      'title' =&gt; $asset-&gt;title,
                      ...
                  ];
              },
          ];
      }
  ]
  </code></pre>
  
  <p>How would I do that for asset fields nested inside a matrix block? I guess I can't use the nested field handle as handle?</p>
  
Tags: <assets><elementcriteriamodel><plugin-elementapi>
Title: How to fetch matrix block items with the ElementAPI
ThreadText: # How to fetch matrix block items with the ElementAPI
  
  (Asked by: username_0 on 2015-10-20)
  
  I have a question regarding the ElementAPI:
  
  I am sucessfully using the ElementAPI to fetch assets related to an entry. Now I have a matrix field where a user can add multiple galleries (multiple block of selected assets) and i want to retrieve the images. But I can't figure out how to use it for nested matrix blocks...
  
  This is how I fetch the data with ElementAPI from non-matrix assets fields (`elementapi.php` config)
  
  ```
  'endpoints' => [
      'api/v1/installation-views/<sourceEntryId:\d+>/<fieldHandle:[A-Za-z]+>' => function($sourceEntryId, $fieldHandle) {
          return [
              'elementType' => 'Asset',
              'criteria' => [
                  'relatedTo' => [
                      'sourceElement' => $sourceEntryId,
                      'field' => $fieldHandle
                  ],
                  'kind' => 'image'
              ],
              'transformer' => function(AssetFileModel $asset) {
                  return [
                      'id' => $asset->id,
                      'title' => $asset->title,
                      ...
                  ];
              },
          ];
      }
  ]
  
  ```
  
  How would I do that for asset fields nested inside a matrix block? I guess I can't use the nested field handle as handle?
  
  **Tags:** assets, elementcriteriamodel, plugin-elementapi
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-10-20. Score: 3 | Accepted answer
  
  Matrix blocks are elements just like entries and assets, so this is just a matter of setting the `elementType` to `'MatrixBlock'`, and adjusting your `criteria` to fetch the block you’re after.
  
  The simplest approach would be to pass the Matrix block ID in the URL, and apply it to the `id` criteria parameter.
  
  ```php
  'endpoints' => [
      'matrix-blocks/<blockId:\d+>' => function($blockId) {
          return [
              'elementType' => 'MatrixBlock',
              'criteria' => [
                  'id' => $blockId
              ],
              'first' => true, // Just return one Matrix block
              'transformer' => function(MatrixBlock $block) {
                  $assetsInfo = [];
  
                  foreach ($block->myAssetsfield as $asset) {
                      $assetsInfo[] = [
                          'url' => $asset->url,
                          'title' => $asset->title,
                          // ...
                      ];
                  }
  
                  return [
                      'id' => $block->id,
                      'assets' => $AssetsInfo,
                      // ...
                  ];
              },
          ];
      }
  ]
  
  ```
  
  You can also use the following criteria parameters to narrow down the search:
  
  * `fieldId` (the Matrix field ID)
  * `ownerId` (the owner element ID, e.g. your entry’s ID)
  * `ownerLocale` (the locale of the owner element that the block should have been saved to)
  * `type` (the block type handle)
  
  > **Comments:**
  > **username_0**: Thank you! Seems easier than I thought. One little question. My matrix block has an assets field. Can I get them directly from $block or would I have to make an additional query?  
  > **username_1**: @RobertKrieg Just updated my answer with an example of adding assets info to the response.  
  
  ---
  
  