OblikStudio / kirby-json

JSON component and field for Kirby.
MIT License
13 stars 0 forks source link

[Feature Request] Ability to Setup a Field with Default Keys/Values #1

Closed luxlogica closed 2 years ago

luxlogica commented 4 years ago

Hi there! Thank you for such a great plugin!

I'm trying to build an interface for users to edit Structured Data in their websites, and I thought the JSON field might be a good fit. Unfortunately, for the kind of use-case I have in mind, rather than giving the user a 'blank' field for them to build a structure from scratch, I'd need to be able to provide them with a ready-made structure, where they fill in (most of) the values.

The idea is that they'd choose the type of Structured Data they need for the page - e.g., 'Local Business' or 'Blog Article' - and I would present them with the appropriate JSON field (with the right key-value pairs already present) for them to fill out.

Is this something that is already possible to do?

If not, then perhaps we could add an extra blueprint option - let's call it 'autobuild' in this example - which the user could use to set out the wanted key-value pairs. Something like this:

autobuild:
  jsonfieldname:
    key: keyname
    value: thevalue
    type: object|array|value(default)

The value could, of course, be a single value, an array, or an object entered as a yaml hierarchy. Specifying a basic 'Person' Structured Data object from schema.org might look like this:

person:
  label: Person Data
  type: json
  keys: false
  values: true
  mutate: false
  autobuild:
    type:
      key: "@type"
      value: Person
    name:
      # we only specify the key, because we purposely want to leave 'value' empty
      key: name
    same:
      # here we leave value empty, but specify the 'type'
      key: sameAs
      type: array
    spouse:
      # here we specify a child-object as the value
      key: spouse
      value:
        type:
          key: "@type"
          value: Person
        name:
          key: name
hdodov commented 4 years ago

I think this should be possible by using the default option in the blueprint, i.e. specifying a default JSON value that the plugin loads and that the user can then modify.

I imagine something like:

myfield:
  type: json
  default: '{"foo": "bar", "name": ""}'

Could you try that and tell if it works? If not, I might need to make some minor changes. But in general, I think this should be the way to go. Perhaps it could be useful to be able to point default to a JSON file and store the default data there instead. Or even, parse the YAML-encoded data as JSON so that this:

myfield:
  type: json
  default:
    foo: bar
    name: ''

However, for structured data that is meant for editing, I think you should try to use structure fields instead. I think the JSON field should be used for generated data that you want to visualize and optionally, allow to edit.

luxlogica commented 4 years ago

Thank you for the explanation, @hdodov!

Unfortunately, the default option doesn't seem to work - I've just tried it with a blank plainkit install, and it didn't work for me... :(

The problem with the Structure Field, is that it presupposes a rigid structure, where every record is the same - not like json. So, in the case of Structured Data for web pages, for example, we don't know in advance which schema the user will need, and we don't want to clutter the content file with - literally - dozens to hundreds of unused fields...

Another option might be for us to use the Kirby Builder plugin - but your plugin seemed closer to our "json needs"! :)

hdodov commented 4 years ago

Yep, the builder plugin is another solution. Perhaps the upcoming Kirby Forms would handle your use-case better. There was a discussion on Slack. Check this.