silverstripe / silverstripe-restfulserver

RestfulServer module for Silverstripe CMS
http://www.silverstripe.org/restfulserver-module/
BSD 3-Clause "New" or "Revised" License
45 stars 48 forks source link

Allow additional config driven fields in JSONDataFormatter #69

Open phptek opened 6 years ago

phptek commented 6 years ago

In JSONDataFormatter line 119 and 151 a baked-in set of three fields are returned for each object in a has_xxx relation which doesn't allow for much in the way of customisation, viz:

                $serobj->$relName = ArrayData::array_to_object(array(
                    "className" => $relClass,
                    "href" => "$href.json",
                    "id" => $obj->$fieldName
                ));

We should allow developers to specify additional fields to return here, declared in YML config viz:

                $serobj->$relName = ArrayData::array_to_object(array_replace([
                    "className" => $relClass,
                    "href" => "$href.json",
                    "id" => $obj->$fieldName
                ], $obj->config()->get('api_fields'));

Note the use of array_replace() so that userland config can also override the defaults, not simply append to them (Not completely sure the latter is a good idea...)

PR

guyvanbael commented 5 years ago

@phptek Is there any documentation on how to configure the fields for the has_many?

phptek commented 5 years ago

Do you want me to update the README and push that up? Let me know.

guyvanbael commented 5 years ago

@phptek That would be nice. Thanx!

markdynamo6 commented 4 years ago

Any word on this yet? It would also be nice to have the option to remove those base fields as well - what's the reason for them? Perhaps these fields should just be the ones as defined in $api_access['index'].

guyvanbael commented 3 years ago

any news on this?