Jump to content
Sign in to follow this  
baronmunchowsen

Get all Repeater Matrix Type Content on Site

Recommended Posts

I have a repeater matrix that is used on multiple pages.

The repeater matrix contains the following repeater matrix types: quote, hero, text, ...

'quote' is made up of 3 fields: quote_text, quote_source, quote_link

I would like to return all instances of the matrix repeater type 'quote' used on any pages. Ideally I would have an array of objects that I could then access the 3 quote fields from.

Is this possible within the processwire field/page api?

Many thanks for any insights.

 

Share this post


Link to post
Share on other sites

Example with a multidimensional array. RepeaterMatrix fieldname = 'fieldname'

  • first level: page where the repeater field lives in, indexed by ID
  • second level: per page repeater items indexed by ID
  • third level: field inside repeater indexed by name
$rp = $pages->find('fieldname.count>0');
$return = [];

foreach ($rp as $p) {
    $return[$p->id] = [];
    // RepeaterMatrixPageArray
    foreach ($p->fieldname as $item) {
        if ($item->type  != 'basic') continue;
        $return[$p->id][$item->id] = [];
        // RepeaterMatrixPage
        foreach ($item->template->fields as $repeaterField) {
            if ($repeaterField == 'repeater_matrix_type') continue; // we do not need this
            if ($item->$repeaterField === '') continue; // if you want to ignore empty strings
            $return[$p->id][$item->id]["$repeaterField"] = $item->$repeaterField;
        }
        // remove if empty
        if (empty($return[$p->id][$item->id])) unset($return[$p->id][$item->id]);
    }
    // remove if empty
    if (empty($return[$p->id])) unset($return[$p->id]);
}

var_dump($return);

 

Share this post


Link to post
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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...