Jump to content
creativejay

Accessing fields inside repeater inside an array of page fields.

Recommended Posts

My brain is probably just tiring out on me right this moment, I'm hoping that by the time I write out my problem I'll see the way through it. If you're reading this, it didn't work. :lol:

Structure in question is:

  1. Series Page
    1. Product pages
      1. Page Fields for each product page
        1. Some of these fields are repeaters
          1. Fields within the repeater

I have made an array of page fields so I don't have to keep track of them all as I develop:

$listings = $page->children; // grab all the published children of the Series page
foreach($listings as $l) { // loop through the children
	foreach($l->fields as $f) { } // loop through each child's page fields where they have a value set
}

Then I break down how to handle each type of field:

if($f->type == 'FieldtypeFile') {  } 
elseif($f->type == 'FieldtypeDatetime'||$f->name == 'prod_status_pages'||$f->type == 'FieldtypeImage'){  } 
elseif($f->type == 'FieldtypePage'){  }

These are all largely working as expected (though I do have a couple of offset/isset exceptions to clean up...)

It's when I get to the repeaters that I run into trouble getting the API calls to work.

Just cycling through the fields as above, the output for a FieldtypeRepeater is the ID of the repeater in that field's array. Everything I read suggests I should treat a repeater the same as I would treat a page, which leads me to the following code.

elseif($f->type == 'FieldtypeRepeater'){ // Repeaters need special treatment otherwise output is just ID
	$th .= "<th><b>{$f->label}</b></th>\n";
	$trows = ""; // creating an empty variable to build my foreach into
	foreach($f->fields as $rf){ // looping, I hope, through the fields of the given Repeater ID
		$trows .= "{$rf->label}: {$f->get($rf)->title} ({$rf->type})<br />\n"; // add an entry to the variable
		}
	$rows .= "<td style='padding: 8px 16px; vertical-align: middle;'>".$trows."<br />\n ({$f->type})</td>\n"; // back out to rendering the Repeater field
}

 

What I would hope would output in the HTML I've been building is something like:
 

<td style='...'>Lo temp: -40 (Integer)<br />
Hi temp: 75 (Integer)<br />
Storage Lo: -40 (Integer)<br />
Storage Hi: 85 (Integer)<br />
Functional to: 85 (Integer)<br />
(FieldtypeRepeater)</td>

So what I'm trying to do here is loop through the populated fields in the unknown Repeater field, and output them as a simple (so far) text list of the repeater.field and its value (and then its type for my reference).

I'm afraid typing this out has fixed some syntax but not enough to get this working as I'd hoped.

Please note not all Repeater fields are integers. Some also have floats, files, or options, and probably a couple others I'm forgetting.

 

I appreciate your time in taking a look at this!

Share this post


Link to post
Share on other sites

Morning (here...),

9 hours ago, creativejay said:

foreach($f->fields as $rf){ // looping, I hope, through the fields of the given Repeater ID

As far as I can see, at this stage you are looping through the Pages of the Repeater and not the fields, yet. You need nested loops:

<?php
foreach($f as $repeater_page) {
	foreach($repeater_page->fields as $rf){ // looping, I hope, through the fields of the given Repeater ID
		//echos...
	}
}

 Warning: written in the browser and without my morning coffee! ;) 

Edited by szabesz
typo

Share this post


Link to post
Share on other sites
6 hours ago, szabesz said:

As far as I can see, at this stage you are looping through the Pages of the Repeater and not the fields, yet. You need nested loops

Thank you for taking a look!

This is still not outputting anything, unfortunately. I'm fussing about with it and will update if I catch on an answer, but if anyone else is taking a look at this I am still seeking the solution.

Thanks!

Share this post


Link to post
Share on other sites

I think you are looking for something like this. You need to be making use of the names of the repeater fields and also referencing the repeater field relative to it's containing page.

foreach($l->{$f->name} as $repeater_item) {
    foreach($repeater_item->fields as $rf) {
        echo $repeater_item->{$rf->name};
    }
}

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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Robin S
      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/RepeaterDepthHelper
      https://modules.processwire.com/modules/repeater-depth-helper/
    • By ICF Church
      Hi 👋
      Anyone else having this problem?
      Requirements:
      - Repeater (matrix & normal) with mutlilanguage fields (text, textarea…) 
      - Backend language set to something other than default (ie. German) 
      Reproduce:
      - Add a new repeater Item (ajax, I found no way to possible to disable it with matrix)

      (Notice how the default language tab is active instead of the backend language…)
      - Write something into the (default language) field
      - Try to save, if field is required, this will not work. If not required, then when reloading, the content will be inside the backend language field, instead of the default language field who was (presumably) active
      Analysis:
      When  loading  a new repeater element with ajax, the default langue tab is active, but the backend language inputfield is visible (with no visual indication). When writing into the field, it will populate the backend language. When manually clicking on the default language tab (which is already active), the field will switch to the actual default language field (which is [now] empty) (that can now be populated…)
      Also Notice, the labels of the elements to be added are in default language as well instead of the translated label (images instead of Bilder)…
      ProcessWire 3.0.148, Profields 0.0.5…
      Is it my system configuration, or does anyone else have the same issue? This is a screen recording of the problem:
      Issue: https://github.com/processwire/processwire-issues/issues/1179

      Screen Recording 2020-02-25 at 14.18.31.mov
    • By neonwired
      I have a front end form for creating new pages, repeater and repeater matrix field don't seem to save any data. I was considering handling the data manually but can't seem to get anything useful from the post data, are there any methods i can use?
    • By neophron
      Hi,
      I'm having trouble with a maybe simple code:
      I created a repeater (gallery_logos_links) and a repeater matrix (RepeaterMatrix_unternehmen). The repeater (gallery_logos_links) is inside the matrix repeater as a matrix type.
      The repeater matrix type is: gallery_logos_links and the image filed from the repeater is single_image.
       
      This my code:
      <?php foreach ($page->RepeaterMatrix_unternehmen as $item) { if ($item->type == 'gallery_logos_links') { echo " {foreach($item->repeater_logos_links as $logo)} <img src='{$logo->single_image->url}' alt='{$logo->single_image->description}' width='400'> {endforeach} "; } else if ($item->type == 'some_stuff') { echo"  
       
    • By shadowkyogre
      [EDIT]: After sitting down and planning out my site according to the ragged hierarchy information, I settled on the following schematics.
      /$world/$template/$content_of_template_type/... for my pretty URLs /roster/$character for my characters a generic Repeater field with depth on most content types for custom positions for child pages to connect to instead of it directly a few Repeater fields on each content that have (PageReference[1], other fields) to establish associations A few FieldsetGroups to help me manage the fields that I needed to copy across a bunch of content types. Kept the original post below for context and tagged the OP for searchability.
      ---
      Hi everyone! I'm working on a personalized worldbuilding wiki to host my art and story stuff.
      Right now my site architecture looks like...
      /$world/cosmology/$cosmology /$world/locations/$location /$world/factions/$faction /$world/history/$history /$world/species/$species /roster/$character So far the layout works, but there's one problem. I need to make sub-sections for an organization. Organizations can appear under cosmology, locations, and factions. Sounds straightforward until... I run into the problem of figuring out how to represent subfactions.
      Key factors in this are...
      Characters should be able to be part of multiple organizations Characters should have an explicit role assigned to their membership. Character pages should be able to query the organization pages to display their ranks across organizations. Editing an organization's hierarchical layout should be visible while editing the root organization page. From what I've read of the ProcessWire documentation, the best use case for each way of representing the organization's subsections are...
      Child Pages:: Works best for menu presentation and dedicated editing. PageTables:: Works if you want control over where to place the PageTable fields, but requires opening a modal for the pages you want to edit. It's also kind of like normal pages. Repeaters:: Works great for inline editing and easy control over hierarchy, but the page urls become obscure. Sections in the body field:: Works for copypasting from my note files. But it doesn't expose relationships for easy querying. It looks like my best case for this is child pages since it allows displaying suborganization in the URL easily. But also I lose out on quickly reordering and editing the child pages. Any advice for people running into similar use cases?
×
×
  • Create New...