getkirby / ideas

This is the backlog of ideas and feature requests from the last two years. Use our new feedback platform to post your new ideas or vote on existing ideas.
https://feedback.getkirby.com
20 stars 0 forks source link

Add option to prefix field names in blueprint #281

Open hdodov opened 5 years ago

hdodov commented 5 years ago

I have a field group groups/section.yml:

type: group
fields:
  sectionTitle:
    type: text
    label: Title
  sectionContent:
    type: textarea
    label: Content

And a page that includes it:

preset: page
fields:
  section: groups/section

That works great, but what if I need another section with exactly the same fields? I can't simply include the section again because it would add the same fields. Should I copy the section and maintain two almost identical files? No, but I've got no choice.

My proposal is to have a prefix option when using groups that automatically prepends a string to all fields' names. Since Kirby works in a case-insensitive manner, that shouldn't be a problem?


This is what I imagine:

groups/section.yml:

type: group
fields:
  title:
    type: text
    label: Title
  content:
    type: textarea
    label: Content

page:

preset: page
fields:
  sectionOne:
    extends: groups/section
    prefix: true
  sectionTwo:
    extends: groups/section
    prefix: true

Which would be the equivalent of adding sectionOneTitle, sectionOneContent, sectionTwoTitle, and sectionTwoContent. This way, I can use a field group unlimited number of times and in any context.

hdodov commented 5 years ago

I often times find myself prefixing a lot of fields that are logically grouped. I usually divide the site in sections and each section has its own prefix so I can differentiate between sectionOne's title and sectionTwo's title. Perhaps we can have a syntactic sugar for prefixing with the use of a dollar sign:

fields:
  heading:
    type: text
  $footer:
    title:
      type: text
    content:
      type: textarea
    links:
      type: structure
      fields:
        link:
          type: url
  text:
    type: textarea

Which could be the same as:

fields:
  heading:
    type: text
  footer:
    type: group
    prefix: true
    fields:
      title:
        type: text
      content:
        type: textarea
      links:
        type: structure
        fields:
          link:
            type: url
  text:
    type: textarea

Which functions like:

fields:
  heading:
    type: text
  footerTitle:
    type: text
  footerContent:
    type: textarea
  footerLinks:
    type: structure
    fields:
      link:
        type: url
  text:
    type: textarea
lukasbestle commented 5 years ago

I like the first idea a lot as it is quite intuitive, fully backwards-compatible and also rather simple to integrate into the current code structure. I'm not so sure about the other idea though – it seems quite complex and would be hard to document and understand.

hdodov commented 5 years ago

@lukasbestle I also had the same concerns, but Kirby encourages a certain style of field naming, while using symbols such as $ is even advised against. Therefore, the people using this would know what they're doing.

I think that saying:

$footer:
  title:

is the same as:

footer:
  type: group
  prefix: true
  fields:
    title:

...would be enough for people to understand the feature clearly - it's just a shorthand. You can define fields in a blueprint without necessarily having to add tabs, columns, and sections before that. This functions in a similar fashion except it utilizes the field name so Kirby can make a difference between field groups and actual fields.

Edit: This even holds true for nested groups. The following:

footer:
  type: group
  prefix: true
  fields:
    test:
      type: text
    title:
      type: group
      prefix: true
      fields:
        bar:
          type: group
          prefix: true
          fields:
            baz:
              type: text

...works in Kirby 3.2.3 and correctly outputs two fields in the panel. With prefixing, it would look like:

$footer:
  test:
    type: text
  $title:
    $bar:
      baz:
        type: text

It would open a whole new dimension in field organization for blueprints. Add to this the fact that you can extend those groups - and you get a lot of useful stuff, in my opinion.

lukasbestle commented 5 years ago

To be honest I disagree – every shorthand we add increases system complexity.