Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The to_json modifier doesn't handle nested query builders #5866

Closed
ottz0 opened this issue Apr 20, 2022 · 8 comments
Closed

The to_json modifier doesn't handle nested query builders #5866

ottz0 opened this issue Apr 20, 2022 · 8 comments

Comments

@ottz0
Copy link

ottz0 commented Apr 20, 2022

Bug description

I've added an Assets field inside of a replicator. I've uploaded 1 or multiple images which adds the image/s to the .md file. I can view and change the images in the CP, however when I view the object it is empty.

How to reproduce

Create a Replicator, add images to the set. Show the image

imageset:
  -
    title: test
    logos:
      - logos/logo1.svg
      - logos/logo1.svg
      - logos/logo1.svg
    type: test1
    enabled: true

{{imageset | to_json | entities }}

Logos shows an empty object, I can view title

[{"title”:”test”,”logos":{},"type”:”test1”},{“title”:”another,”logos":{},"type”:”test”2}]

Logs

No response

Versions

php 8.1

Installation

Fresh statamic/statamic site via CLI

Antlers Parser

No response

Additional details

Laravel Sail

@jasonvarga
Copy link
Member

The issue here is that each assets field is a query builder instance.

So it's moreso a problem with the to_json modifier.

Are you actually intending on just smushing the whole grid into a JSON object like that or were you just playing around before using it another way?

@jasonvarga jasonvarga changed the title Assets in Replicator The to_json modifier doesn't handle nested query builders Apr 20, 2022
@ottz0
Copy link
Author

ottz0 commented Apr 21, 2022

I'm just trying to display an image/s inside of a replicator set. I want to pass the whole replicator set including the images into a vue.js component so I can then loop through the replicator set, so I need to the to_json to pass it to vue.js.

So Ideally as 1 big object, however the images inside the replicator set are only the query builder instance (a reference) and not the image data itself, so I need to then loop through these again (to get the actual image data) to somehow pass it to my vue.js component

<servers :set="{{ imageset | to_json | entities }}" />

@afonic
Copy link

afonic commented Apr 28, 2022

I have the same problem with 3.3.x. I think it was working OK in 3.2, I will need to check.

Until then I've created a custom modifier to augment these query builders before json_encode.

@afonic
Copy link

afonic commented May 23, 2022

Generally since 3.3.x several of our websites have issues. All of them have fairly complicated setups for example a Replicator field that has a set with an Entries field.

In this Entries field when using 3.2.x both to_json and ->toAugmentedArray() (when using in a Tag), the values get passed just fine. In 3.3.x both are empty. I'll probably investigate and open a new issue but they seem related.

@ryanmitchell
Copy link
Contributor

Seems related to: #6074

@afonic
Copy link

afonic commented May 24, 2022

They don't seem related, but I could be wrong.

It seems related to this change: https://statamic.dev/upgrade-guide/3-2-to-3-3#entries-fieldtype

My guess, when this query build is nested inside another fieldtype it's not being augmented correctly when passed to a modifier like to_json.

However since {{ field | to_json | entities }} is probably the best way to pass data to something like Vue I would like to learn if this can / will be fixed, otherwise we need to make drastic changes, blueprint-wise.

@duncanmcclean
Copy link
Member

I've managed to reproduce this bug and created a repository demonstrating it (from #6130)..

@duncanmcclean
Copy link
Member

This has been added/fixed in Statamic 5, which we're hoping to release in the next few days/weeks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants