verbb / super-table

Super-charge your Craft workflow using Super Table.
MIT License
317 stars 47 forks source link

Static ST Fields || Dropdown Defaults not populating #531

Open DynamiteGoesBoom opened 1 year ago

DynamiteGoesBoom commented 1 year ago

Describe the bug

I recently asked Brandon Kelly about this but I think since this issue is only happening within Static SuperTable fields, this is probably a better place.

So I have a bunch of these fields under a Style tab (thank you Matrix Mate!). Inside of that I have a slew of options. Some of those options have Dropdowns with a default value checked.

Like BK said, Craft will only populate those on new fields. But the issue is that sometimes I add new fields to existing field layouts. The problem with that is then after deployments, I need to manually update those values since they were added to existing field layouts.

I know with Craft 4.5 they plan on improving how Craft handles those, but is there anything that can be done within SuperTable to set the initial value to what's Default if the field is empty?

Steps to reproduce

  1. Create a static supertable field
  2. Save
  3. Add a new dropdown with some values and select one to be Default
  4. check the entry you assigned it to as the new dropdown will be empty.

Craft CMS version

4.4.17

Plugin version

3.0.9

Multi-site?

No response

Additional context

No response

engram-design commented 1 year ago

Hmm, I suppose it's possible, but I would think this be the responsibility of the Dropdown field to set the default for new fields added to existing blocks.

I suppose we need a mechanism to determine whether the field has no value because it's never been saved, or doesn't have a value because the editor has (rightly) not given the field value yet. Otherwise, we'll keep falling over things by setting the default option when none is picked.

DynamiteGoesBoom commented 1 year ago

Thanks for the info @engram-design. I was initially wondering if there was an event you could use that fires when a new field gets adding via PC? That, to me, would be a perfect time to check if there's a default value checked. If there is, then just set that default value as the actual value. BK made it somewhat clear that the only times that the default value would get set is if it's a new field which in most cases will never be with a Static Super Table field. Hence why I was hopeful the mighty Crawf could help out here 💪

Thanks for all that you do!

engram-design commented 1 year ago

The problem with that approach is that it needs to do that on every environment, because adding the field on dev, needs to push content to the element on the environment for dev/staging/prod/etc.

There is the concept of isFresh() for both an element and a field, but that won't apply here, because the block (the element) isn't fresh - it's already been created. There's just no value for the field.

We can't use if DropdownField == null because a null value can represent an empty option (no value picked).

I'll give it some more thought.

DynamiteGoesBoom commented 1 year ago

Thanks for the continued thoughts on this as it would be a big help. Could you test if dropdownField == null && if there's a default value checked? Theoretically I think that could work but you're the pro 🎉

I guess I was thinking that PC runs it's migrations on craft up that this could be possible, but curious on what can be done, I was also thinking that some sort of config override setting could be useful if developers wanted to change out that functionality but that's probably a bit overkill.

Thanks again!