RepeaterDepthHelper by Robin S

Enforces some depth rules for Repeater fields on save, and provides a helper method that returns a nested depth structure for a Repeater field value.

Repeater Depth Helper

A module for ProcessWire CMS/CMF. 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

Install and use modules at your own risk. Always have a site and database backup before installing new modules.

Latest news

  • ProcessWire Weekly #522
    In the 522nd issue of ProcessWire Weekly we'll check out the latest core updates, introduce a new module called File Mover, and more. Read on!
    Weekly.pw / 11 May 2024
  • ProFields Table Field with Actions support
    This week we have some updates for the ProFields table field (FieldtypeTable). These updates are primarily focused on adding new tools for the editor to facilitate input and management of content in a table field.
    Blog / 12 April 2024
  • Subscribe to weekly ProcessWire news

“We were really happy to build our new portfolio website on ProcessWire! We wanted something that gave us plenty of control on the back-end, without any bloat on the front end - just a nice, easy to access API for all our content that left us free to design and build however we liked.” —Castus, web design agency in Sheffield, UK