craftcms-prune icon indicating copy to clipboard operation
craftcms-prune copied to clipboard

Allow for nested field types (entries, matrix, etc)

Open timkelty opened this issue 10 years ago • 6 comments

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 or entires), and which fields or columns to get.
  • a true instead of a map on an entries field could return an array of IDs.
  • a true instead of a map on an matrix 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 }}

timkelty avatar Oct 15 '14 13:10 timkelty

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.

timkelty avatar Oct 15 '14 14:10 timkelty

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

jlawrence-yellostudio avatar Oct 15 '14 14:10 jlawrence-yellostudio

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.

mattstauffer avatar Jan 31 '15 02:01 mattstauffer

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 avatar Aug 27 '15 01:08 howardroark

@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 avatar Aug 27 '15 12:08 timkelty

@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.

howardroark avatar Aug 28 '15 16:08 howardroark