mattstauffer / craftcms-prune

CraftCMS Prune Twig plugin
49 stars 2 forks source link

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

Open timkelty opened 9 years ago

timkelty commented 9 years ago

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.

{{ 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 commented 9 years ago

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.

jlawrence-yellostudio commented 9 years ago

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

mattstauffer commented 9 years ago

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.

howardroark commented 9 years ago

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.

timkelty commented 9 years ago

@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

howardroark commented 9 years ago

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