craftcms-prune
craftcms-prune copied to clipboard
Allow for nested field types (entries, matrix, etc)
Just making an issue here to brainstorm. I was playing around with the best way to approach this and was first considering how we'd want it to look from the template side.
Option 1:
Make the fields param a hash.
-
true
would indicate just returning the value. - a hash would indicate this field had nested data (e.g.
matrix
orentires
), and which fields or columns to get. - a
true
instead of a map on anentries
field could return an array of IDs. - a
true
instead of a map on anmatrix
field could return a hash of every column and it's value.
{{ craft.entries.section('news').find() | prune({
'id': true
'title': true,
'myEntriesField': {
'id': true,
'otherField': true,
'myNestedMatrixField': true,
},
'myMatrixField': {
'myCol': true,
'myEntriesCol': {
'id': true,
'title': true,
}
},
'myOtherEntriesField': true
}) | json_encode() | raw }}
Option 2:
Add a 3rd mappings
param, with the keys of the field and the value of the fields/columns you want.
{{ craft.entries.section('news').find() | prune(
['id', 'title', 'myEntriesField', 'myMatrixField', 'myOtherEntriesField'], {
'myEntriesField': ['id', 'otherField', 'myNestedMatrixField'],
'myMatrixField': ['myCol', 'myEntriesCol'],
'myEntriesCol': ['id', 'title'],
}) | json_encode() | raw }}
It looks like some work has been done here: https://github.com/mattstauffer/craftcms-prune/pull/1
With that approach however, an entries field will dump out every attribute from the ElementCriteriaModel
, which certainly isn't what you want.
Yes my note states that its not yet pruned, I was suggesting the use of a dot notation to prune nested data. e.g
entry.type.somefield
See note on the open PR. I absolutely never got notified on this. SO SORRY. Let me know where you all landed on this and I'll gladly merge the appropriate thing in.
Hey @timkelty I like your option #2... but I think the object at the end should really be three separate objects. Then keep the returned object one dimensional, using the first key of each passed object as the key in the output.
I don't really agree with the dot notation just because JSON is being passed to the twig plugin and that is pretty easy to understand already.
@howardroark That makes sense too.
In practice (and the time since this discussion started), for anything more complicated than just yanking a few fields out into json, I would just use https://github.com/pixelandtonic/ElementAPI
@timkelty Yeah, that does look like a very nice option for custom stuff :)
Though it is still great to have a super simple way to build an object of your entries. The trick is getting a sense of the most common use cases.