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.

| improve this question | | | | |
  • I think the Craft docs cover this quite nicely? craftcms.com/docs/matrix-fields#templating – mrsmn Aug 2 '17 at 11:36
  • 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 Aug 2 '17 at 11:50
  • Ow my bad, completely overlooked that part... let me see if I can dig something up – mrsmn Aug 2 '17 at 12:02

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.

| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.