<p>I think I've figured out how to do this now (mainly through trial and error).
The code above should actually be like this:</p>

<pre><code>foreach ($matrixBlocks as $block) {

    foreach ($block-&gt;blockContent as $blockContent) {
        ....
    }
    foreach ($block-&gt;blockDependency as $blockDependency){
        ....
    }
}
</code></pre>

<p>And the the <code>$blockContent</code> and <code>$blockDependency</code> variables will contain the entries required.</p>

CC BY-SA 3.0
2017-08-02T14:30:28.477Z
2017-08-03T06:23:21.317Z
2017-08-03T06:23:21.317Z
Jai Redden
<p>I am writing a plugin function that takes an entry id, the entry has a matrix field (called block) and the matrix has a blocktype which contains a number field (blockStep), an entries field (blockContent) and another entries field (blockDependency).</p>

<p>I am getting the entry, which i am calling a module, like this:</p>

<pre><code>$module = craft()-&gt;entries-&gt;getEntryById($moduleid);
</code></pre>

<p>This works fine. Then I get the matrix field, called block, like this:</p>

<pre><code>$matrixBlocks = $module-&gt;block;
</code></pre>

<p>This also works fine. However, when I try to loop through the matrix field, like this:</p>

<pre><code>    foreach ($matrixBlocks as $block){
      var_dump( $block-&gt;getContent()-&gt;blockStep);
      var_dump( $block-&gt;getContent()-&gt;blockContent);
      var_dump( $block-&gt;getContent()-&gt;blockDependency);
    }
</code></pre>

<p>Only the blockStep, which is a number, is set - the two entries fields are just returning <code>NULL</code>.</p>

<p>Can someone help? How should I be accessing these entries fields?</p>

<p>Thank you.</p>

CC BY-SA 3.0
2017-08-02T11:36:57.910Z
I think the Craft docs cover this quite nicely? https://craftcms.com/docs/matrix-fields#templating
mrsmn
CC BY-SA 3.0
2017-08-02T11:50:51.540Z
Those docs cover templating, this is in plugin development. Can you tell me how to do it in a plugin? I have obviously checked the docs, but I cant find how to do it.
Jai Redden
CC BY-SA 3.0
2017-08-02T12:02:28.493Z
Ow my bad, completely overlooked that part... let me see if I can dig something up
mrsmn
<plugin-development><matrix><entries-field>
Accessing entries in a matrix field in plugin
# Accessing entries in a matrix field in plugin

(Asked by: username_0 on 2017-08-02)

I am writing a plugin function that takes an entry id, the entry has a matrix field (called block) and the matrix has a blocktype which contains a number field (blockStep), an entries field (blockContent) and another entries field (blockDependency).
I am getting the entry, which i am calling a module, like this:
$module = craft()->entries->getEntryById($moduleid);

This works fine. Then I get the matrix field, called block, like this:
$matrixBlocks = $module->block;

This also works fine. However, when I try to loop through the matrix field, like this:
    foreach ($matrixBlocks as $block){
      var_dump( $block->getContent()->blockStep);
      var_dump( $block->getContent()->blockContent);
      var_dump( $block->getContent()->blockDependency);
    }

Only the blockStep, which is a number, is set - the two entries fields are just returning NULL.
Can someone help? How should I be accessing these entries fields?
Thank you.

**Tags:** plugin-development, matrix, entries-field

> **Comments:**
> **username_1**: I think the Craft docs cover this quite nicely? https://craftcms.com/docs/matrix-fields#templating  
> **username_0**: Those docs cover templating, this is in plugin development. Can you tell me how to do it in a plugin? I have obviously checked the docs, but I cant find how to do it.  
> **username_1**: Ow my bad, completely overlooked that part... let me see if I can dig something up  

---

## 1 answers

### Answer by username_0 on 2017-08-02. Score: 2

I think I've figured out how to do this now (mainly through trial and error).
The code above should actually be like this:
foreach ($matrixBlocks as $block) {

    foreach ($block->blockContent as $blockContent) {
        ....
    }
    foreach ($block->blockDependency as $blockDependency){
        ....
    }
}

And the the $blockContent and $blockDependency variables will contain the entries required.


---

