Jump to content

Repeater Depth Helper


Robin S
 Share

Recommended Posts

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:

  1. It enforces some depth rules for Repeater fields on save. Those rules are:

    1. The first item must have a depth of zero.
    2. Each item depth must not be more than one greater than previous item depth.
  2. 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

rdh-tracy

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);

rdh-list

 

https://github.com/Toutouwai/RepeaterDepthHelper
https://modules.processwire.com/modules/repeater-depth-helper/

  • Like 12
  • Thanks 3
Link to comment
Share on other sites

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)

  • Like 1
Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...