Hi,
using Kirby Modules I have nested a module inside a module (I need that level of indentation for a survey).
The page is correctly created in the blueprint.
When I tried to adjust the module-name.html.php
to fit this level of depth, by using grandChildren()
<li>
<?php foreach($module->grandChildren()->builder()->toStructure() as $section): ?>
<?php snippet('sections/' . $section->_fieldset(), array('data' => $section)) ?>
<?php endforeach ?>
</li>
I get an error message saying the object is null
.
- Is there a better way to nest modules within modules?
- How can I print all pages and sub-pages part of a module?
If $module
is the module page, and the nested module is a subpage of that module, shouldn’t it be child of $module
instead of a grandchild?
Yes, I first tried that but I got the same error.
I also tried to test it with only one of the two modules who has a subpage module, but same problem
Call to a member function toStructure() on null
Oh, having a fresh look often helps. In fact, it’s not that surprising that it does not work: you are trying to get the builder field from a collection; you have to loop through the collection first and then call the builder field on each page, of course:
<?php foreach($module->children() as $child): ?>
<?php $builder = $child->builder()->toStructure(): ?>
<?php foreach($builder as $section): ?>
<?php snippet('sections/' . $section->_fieldset(), array('data' => $section)) ?>
<?php endforeach ?>
<?php endforeach ?>
Right! Thanks.
This approach works, I actually replaced ->children()
with ->grandChildren()
and it picks up the subpage correctly.
<li>
<fieldset class="bd--a_red pd--v_1" id="<?php echo $module->num() ?>">
<?php foreach($module->builder()->toStructure() as $section): ?>
<?php snippet('sections/' . $section->_fieldset(), array('data' => $section)) ?>
<?php endforeach ?>
<?php foreach($module->index() as $child): ?>
<?php $builder = $child->builder()->toStructure(); ?>
<?php foreach($builder as $section): ?>
<?php snippet('sections/' . $section->_fieldset(), array('data' => $section)) ?>
<?php endforeach ?>
<?php endforeach ?>
</fieldset>
</li>