-
Notifications
You must be signed in to change notification settings - Fork 667
JSON field type #16809
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
JSON field type #16809
Conversation
[ci skip]
I'm not totally sure what the use-case is for this, but it might be worth allowing a field setting for a JSON schema to validate against. Perhaps using https://github.com/jsonrainbow/json-schema Not sure if it's worth the technical debt, just wanted to throw it out there. |
Could just be using https://github.com/nystudio107/craft-code-editor for the text input field (rather than Code Mirror), as you did for the settings for CK Editor. Then you get the Monaco (VS Code) editor, with auto-complete, etc. |
@Mosnar Yeah crossed my mind! Ideally we’d be able to do it in the editor as well, e.g. using codemirror-json-schema. That’s for CM 6 and we’re currently stuck on v5 because of GraphiQL, but I tasked @brianjhanson with updating that to v3 + CM 6 earlier today. @khalwat 213KB vs. 63.5MB 🫤 |
Yeah the build is large, but that's only because as a general editor, I don't know what parts of it people are going to end up using, so it's a kitchen-sink approach. Also since it runs via web workers, it's actually super efficient, it's just disk space. The good news is, if y'all want to make a custom build of it, you can do that, it's all OSS. PRs/improvements welcome! For example, a good bit of the size of the build is a result of:
Monaco is always going to be larger than Code Mirror (in large part because it does more), but if you really care about the space on disk, there's plenty of (arguably useful) fat that could be trimmed. |
Also given that So it might be nice to consolidate efforts and improve Made an issue here so as not to derail this discussion: nystudio107/craft-code-editor#12 |
@Mosnar the Monaco editor supports JSON schemas as well, they are implemented in the CKEditor plugin. Docs: https://github.com/nystudio107/craft-code-editor?tab=readme-ov-file#json-schema-autocompletes |
Description
Adds a new “JSON” field type, which displays a simple CodeMirror input for entering JSON data.
All JSON value types are allowed: arrays, objects, and scalar values.
Tempalting
JSON field values are available to templates as
craft\fields\data\JsonData
objects, which implement ArrayAccess and IteratorAggregate, so they can be treated like arrays.They also have the following methods:
getType()
– returnsboolean
,integer
,float
,string
, orarray
getValue()
– returns the raw valuegetJson()
– returns the JSON-encoded valuegetJson()
accepts two optional arguments:pretty
– whether the JSON value should be prettifiedindent
– the indent string that should be used, e.g." "
or"\t"
(" "
by default)GraphQL
JSON fields will always return either the JSON-encoded value, or
null
, when queried via GraphQL.