Jump to content

Get list of fields and values from a repeater


nabo
 Share

Recommended Posts

Hello, this is my code 

        $p = $pages->get("/".$pageId."/");

        if($p->id) {
          $pdata = ["id" => $pageId]; // array for storing page data with added page id
          $p->of(true); // set output formatting to false before retrieving page data

          // loop through the page fields and add their names and values to $pdata array
          foreach($p->template->fieldgroup as $field) {
            if($field->type instanceof FieldtypeFieldsetOpen) continue;
            $value = $p->get($field->name); 

            switch ($field->type) {

              case 'FieldtypeOptions':
                $pdata[$field->name] = $p->get($field->name)->title;
                break;

              case 'FieldtypeFieldsetPage': 
                $rdata = [];
                foreach ($field->fields as $f) {
                  $rdata[$f->name] = $f->get($f->name);
                }

                $pdata[$field->name] = $rdata;
                break;

              default:
                $pdata[$field->name] = $field->type->sleepValue($p, $field, $value);
                break;
            }
          }

          $response = $pdata;

I'm trying to get all fields and values from a FieldtypeFieldsetPage (I think that for a repeater is the same)... but it doesn't work... I got ALL fields with null value

Link to comment
Share on other sites

 $p = $pages->get("/".$pageId."/");

I think with this syntax you might be trying to retrieve a path with the ID, which i don't think will work unless your page name and tree location actually matches the Page ID, try just getting it like this:

 $p = $pages->get($pageId);
Link to comment
Share on other sites

11 minutes ago, nabo said:

Thank @elabx but the problem is not there.

It's here


case 'FieldtypeFieldsetPage': 
                $rdata = [];
                foreach ($field->fields as $f) {
                  $rdata[$f->name] = $f->get($f->name);
                }

                $pdata[$field->name] = $rdata;
                break;

 

I think you are iterating the repeater items assuming they are like fields, so when you assign $rdata, the array is indexed by the page name (each item being a name), and $f->get($f->name) is trying to get a field, but by using a page ID, maybe that's why it's not getting anything. 

 

Link to comment
Share on other sites

37 minutes ago, nabo said:

Thanks... but, again, this is not problem since I get ALL fields with this foreach


foreach ($field->fields as $f) {

instead of fields related to fieldsetPage

Sorry, my bad, understood the problem wrong from the beginning. Just tested a similar code on an installation I have. 

I think this line should fix it i think you where trying to get the value from $f which is just the Field object data.

 $rdata[$f->name] = $field->get($f->name);

 

Link to comment
Share on other sites

SOLVED

case 'FieldtypeFieldsetPage': 

	$rdata = [];
	$rdata["label"] = $field->$label ? $field->$label : $field->label;
	
	foreach ($field->repeaterFields as $i) {
		$l = wire('fields')->get($i)->$label ? wire('fields')->get($i)->$label : wire('fields')->get($i)->label;
		$rdata[$l] = $p->$field->get($i);
	}

	$pdata[$field->name] = $rdata;

break;

 

  • 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

  • Recently Browsing   0 members

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