nabo Posted June 18, 2018 Share Posted June 18, 2018 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 More sharing options...
elabx Posted June 18, 2018 Share Posted June 18, 2018 $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 More sharing options...
nabo Posted June 18, 2018 Author Share Posted June 18, 2018 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; Link to comment Share on other sites More sharing options...
elabx Posted June 18, 2018 Share Posted June 18, 2018 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 More sharing options...
nabo Posted June 18, 2018 Author Share Posted June 18, 2018 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 Link to comment Share on other sites More sharing options...
Tom. Posted June 18, 2018 Share Posted June 18, 2018 Hi nabo, You tried print_r the data so you can see exactly what's being output? Link to comment Share on other sites More sharing options...
nabo Posted June 18, 2018 Author Share Posted June 18, 2018 21 minutes ago, Tom. said: Hi nabo, You tried print_r the data so you can see exactly what's being output? nothing Link to comment Share on other sites More sharing options...
elabx Posted June 18, 2018 Share Posted June 18, 2018 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 More sharing options...
nabo Posted June 18, 2018 Author Share Posted June 18, 2018 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; 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now