Conditional field in structure not working... what am I doing wrong?

I’m working on a custom “image grid” block. I know there’s a built-in “Image Gallery” block but unfortunately the details of what I’m needing are quite different.

I’ve run into an issue with the conditional (when) property:

  imageGrid:
    name: Image Grid
    icon: dashboard
    fields:
      image_interaction_type:
        label: Image Interaction Type
        type: radio
        default: lightbox
        options:
          lightbox: Lightbox
          link: Link
      images: 
        label: Images
        type: structure
        fields:
          link: 
            type: url
            # Problem here... This is not working
            when: 
              image_interaction_type: 'link'

This should be working, no? What might I be doing wrong?

Maybe the problem is related to the conditional field being in a structure (and the condition referencing a field outside of it, in the block’s context)?

The goal is to only show the link field in the structure (each image in the grid) if the block’s image_interaction_type is set to link.

Grateful for any insight!

1 Like

like you said it can not fetch data from outside the scope. consider using this plugin by @rasteiner GitHub - rasteiner/k3-whenquery: Conditionally show fields and sections. Better. instead which has support for finding the context of the model.

I fear whenquery won’t work either. It only works if the field is either in the same fieldset (like any field that is together in the structure with the link field) or a field at the page root level, but not an “intermediate” level like the block form.

Unfortunately it doesn’t look that trivial to get “correct” scoping working with how the panel currently works. Maybe the core when property has a similar problem…

1 Like

Thanks for the feedback, @bnomei and @rasteiner.

Yes, I tried whenquery before posting here and it also didn’t work.

This is a real bummer. Seems like such a simple usecase… surprised this hasn’t come up before and that it’s not currently supported.

Any ideas on potential workarounds?

I don’t have time to try it out, but what about having a hidden AND disabled field inside the structure that gets its value from a outside the scope, and then base the condition on this intermediate field instead?

I kind of have the same issue in Kirby 4. Did anyone find a solution or workaround for this?

A workaround could be to use two different structure fields depending on the outer value.

imageGrid:
    name: Image Grid
    icon: dashboard
    fields:
      image_interaction_type:
        label: Image Interaction Type
        type: radio
        default: lightbox
        options:
          lightbox: Lightbox
          link: Link
      images1: 
        label: Images
        type: structure
        when:   image_interaction_type: link
        fields:
          link: 
            type: url
      images2: 
        label: Images
        type: structure
        when:   image_interaction_type: lightbox
        fields:
          link: 
            type: url

Not as clean as the desired result, particularly if there are more options that do different things, but can sometimes do the job.

Thank you Sonja. I have already considered this, but unfortunately duplicates to much code on my end. My structure is a bit too… big :sweat_smile: Maybe using a structure is not the best choice in my case in the first place. But thanks anyway!