Jump to content

How to hook repeater or repeater matrix field output


Recommended Posts

I've got some code that is carrying out a basic find-and-replace operation on some page output. I'm hooking into the page render:

$this->addHookBefore('Page::render', $this, 'renderOutput');

Looping through the fields and modifying page output accordingly which is all working fine.

The problem is I cannot figure out what approach I should be using for doing the same on repeater or repeater matrix fields in the page output. As far as I can tell Page::render hook doesn't fire for the repeater pages so I presume I need to loop through the matrix item sub fields at output.

Does anyone have any guidance/examples - I can only find examples for hooking repeaters on CMS side rather than frontend output.

Link to comment
Share on other sites

SOLVED - but I'll leave this here for anyone else to reference as I struggled to find examples.

Long story short, I think I was massively overthinking it - with a bit of help from Chat GPT (!! Chat GPT know ProcessWire now - who knew!) I've got this:

 

.....
} elseif($field->type == 'FieldtypeRepeaterMatrix') {
            //HANDLE REPEATERS HERE
            $repeaterItems = $page->$field;
            foreach ($repeaterItems as $item) {
                // Iterate over each field in the Repeater Matrix item
                foreach ($item->fields as $subField) {
                    $subFieldName = $subField->name;
                    $subFieldValue = $item->get($subFieldName);

                    // Perform your modification here if the subfield is a string type
                    if (is_string($subFieldValue)) {
                        $modifiedValue = $this->replace_merge_tags($subFieldValue, $tags, $defaultTags);
                        // Save the modified content back to the item
                        $item->set($subFieldName, $modifiedValue);
                    }
                }
            }
        }

 

  • Like 2
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...