nabo Posted June 18, 2018 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
elabx Posted June 18, 2018 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);
nabo Posted June 18, 2018 Author 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;
elabx Posted June 18, 2018 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.
nabo Posted June 18, 2018 Author 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
Tom. Posted June 18, 2018 Posted June 18, 2018 Hi nabo, You tried print_r the data so you can see exactly what's being output?
nabo Posted June 18, 2018 Author 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
elabx Posted June 18, 2018 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);
nabo Posted June 18, 2018 Author 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
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