Martin Muzatko Posted February 9, 2017 Share Posted February 9, 2017 Hello! I'm building a REST API utilizing Processwire. A problem I currently encounter, is that I need all data from fields. Either I create different objects depending on the field type (which would suck) or I get all the data that I require in the first place with something like (array) $page->fieldname; In Processwire, I would access the title of the select field via $page->fieldname->title so I don't just get the value 1 or 2, but the mapped name of that select option. See specifically the code for the field: 'field' => (array) $page->{$field->name} I'll share my code for GET method below case 'GET': $json = []; $connections = []; foreach ($page->children as $key => $child) { $connections[$child->id] = $child->httpUrl; } $additionalFields = [ 'created' => $page->created.'000', 'published' => $page->published.'000', 'modified' => $page->modified.'000', 'createdUser' => $page->createdUser->name, 'modifiedUser' => $page->modifiedUser->name, 'parent' => (string) $page->parent, 'template' => $page->template->name ]; $json['children'] = $connections; foreach ($page->fields as $field) { $json[$field->name] = [ 'value' => htmlentities($page->{$field->name}), 'field' => (array) $page->{$field->name} ]; } foreach ($additionalFields as $field => $value) { $json[$field] = [ 'value' => $value ]; } break; No matter with what I try to access the $page->{$field->name}. I never get to figure out that I can use "->title" I tried: get_object_vars() get_class_methods() json_decode(json_encode()) (array) and even Reflections to no success. Any idea what I can do to get ALL the data in a JSON friendly manner? See attached the whole code. Thank you already in advance! json.php Link to comment Share on other sites More sharing options...
LostKobrakai Posted February 9, 2017 Share Posted February 9, 2017 1 hour ago, Martin Muzatko said: No matter with what I try to access the $page->{$field->name}. I never get to figure out that I can use "->title" $f = $page->{$field->name}; $title = $f->title; Link to comment Share on other sites More sharing options...
Martin Muzatko Posted February 9, 2017 Author Share Posted February 9, 2017 Hello LostKobrakai. That is what I can't do, as I want a generic approach for all data. It does work for this one instance where the field is a SelectOptionInputField. but It will throw errors for other fields. I want a generic solution to get all the data for fields. If I try to retrieve the data No matter if it returns a string or an object with a title. Having to individually pick information depending on FieldClass would mean a lot of extra effort - I want the front-end to handle this. But no matter how I try to get access to the field data, I never have access to the title. This is how the field is set up: Example: Using 'field' => (array) $page->{$field->name} \ Maybe the data in *data.1 is lost because I cast it to a one-dimensional array. 'field' => json_decode(json_encode($page->{$field->name}), true) does not yield any data at all for the field. What am I doing wrong? Thank you in advance. Link to comment Share on other sites More sharing options...
kongondo Posted February 9, 2017 Share Posted February 9, 2017 @Martin Muzatko. This might be of interest? 1 Link to comment Share on other sites More sharing options...
LostKobrakai Posted February 9, 2017 Share Posted February 9, 2017 I'm not sure you can get away with a one-size-fits-all solution. Data of fields just is different and therefore in different shapes. Link to comment Share on other sites More sharing options...
Robin S Posted February 9, 2017 Share Posted February 9, 2017 4 hours ago, Martin Muzatko said: Having to individually pick information depending on FieldClass would mean a lot of extra effort Why is it a lot of extra effort? There's not that many field classes, and you would know which are in use in your installation and just check for those, for each returning the data that suits that class. 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