Robin S Posted June 11, 2020 Share Posted June 11, 2020 This is a module I made as an experiment a while ago and never got around to releasing publicly. At the time it was prompted by discussions around using Repeater fields for "page builder" purposes, where the depth feature could possibly be used for elements that would be nested inside other elements. I thought it would be useful to enforce some depth rules and translate the depth data into a multi-dimensional array structure. I'm not using this module anywhere myself but maybe it's useful to somebody. Repeater Depth Helper This module does two things relating to Repeater fields that have the "Item depth" option enabled: It enforces some depth rules for Repeater fields on save. Those rules are: The first item must have a depth of zero. Each item depth must not be more than one greater than previous item depth. It provides a RepeaterPageArray::getDepthStructure helper method that returns a nested depth structure for a Repeater field value. Helper method The module adds a RepeaterPageArray::getDepthStructure method that returns a multi-dimensional array where the key is the page ID and the value is an array of nested "child" items, or null if there are no nested children. Example The module doesn't make any assumptions about how you might want to use the depth structure array, but here is a way you might use it to output a nested unordered list. // Output a nested unordered list from a depth structure array function outputNestedList($depth_structure, $repeater_items) { $out = "<ul>"; foreach($depth_structure as $page_id => $nested_children) { $out .= "<li>" . $repeater_items->get("id=$page_id")->title; // Go recursive if there are nested children if(is_array($nested_children)) $out .= outputNestedList($nested_children, $repeater_items); $out .= "</li>"; } $out .= "</ul>"; return $out; } $repeater_items = $page->my_repeater; $depth_structure = $repeater_items->getDepthStructure(); echo outputNestedList($depth_structure, $repeater_items); https://github.com/Toutouwai/RepeaterDepthHelperhttps://modules.processwire.com/modules/repeater-depth-helper/ 13 3 Link to comment Share on other sites More sharing options...
maddmac Posted June 11, 2020 Share Posted June 11, 2020 Outstanding Robin, this looks amazing, I am definitely going to try this out. 1 Link to comment Share on other sites More sharing options...
Ivan Gretsky Posted June 15, 2020 Share Posted June 15, 2020 Super cool stuff, @Robin S! Your modules are always the thing you wanted to, but never managed to do) I understand it is for all Repeaters not for Repeater Matrix only. But when used with Repeater Matrix it would be useful to: limit the RM types for the 0 level; limit the available children for a type. Maybe it is too much. And would probably require hooks to implement anyway, so not so useful for easy management. But just wanted to get those ideas out of my head) 1 Link to comment Share on other sites More sharing options...
Robin S Posted June 15, 2020 Author Share Posted June 15, 2020 16 hours ago, Ivan Gretsky said: But when used with Repeater Matrix it would be useful to: limit the RM types for the 0 level; limit the available children for a type. Those are really feature requests for Repeater Matrix rather than relating to what this module is focused on. You could validate the Repeater Matrix field with a Pages::saveReady hook - you'd loop over each repeater item and check its matrix type and level, and give the user feedback if they make an error. But you'd be relying on the user correcting mistakes afterwards because you probably wouldn't want to be automatically deleting repeater items that are in error. If you are talking about Javascript validation then that would be more complex. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now