Jump to content

Field dependency on Image field


evan
 Share

Recommended Posts

Do field dependencies work on Image fields?  I tried this selector, to show a field if there's an image:

image_field.count > 0

...and it didn't work.  Any ideas?  Thanks!

-evan

Link to comment
Share on other sites

You don't need dependencies for this to work if you want to show the image_field itself only when there are images.

In the field settings for your image_field go to input tab -> visibility and choose "Collapsed only when blank". This should do what you want.

EDIT:

I just tried this on a 2.5.29 dev install and I can confirm that it is not working as expected.

I have a field images and a field bodycopy.

For bodycopy I choose "Show this field only if..." images.count>0

The bodycopy doesn't show at all, no matter if there is an image in the images field or not.

The field is there in the HTML with an attribute data-show-if='images.count>0' in the field wrapper.

Maybe the JS cannot compute the >0 part. But this is just a guess and needs further investigation.

Should we file this as bug?

EDIT2:

I tested this in a multi language setup. In the dependencies documentation section limitations it says

Field dependency selectors may not yet query the value of a multi-language field. However, they can show/hide multi-language fields according to the value of other fields.

Do you also have a multi language setup?

Link to comment
Share on other sites

I'm actually trying to show or hide fields only if there's an image uploaded to the image field, not just collapse an empty image field, as you tried it.  I'm not using a multi-language setup.

I'm pretty sure it's not just a parse error, as other selectors also fail, such as:

image_field != false
image_field.count = 0
Link to comment
Share on other sites

I was not sure from your first post whether you just wanted to collapse the field when empty. That is why I included those instructions.

Further down in my first post I describe that I have the same behaviour here on a 2.5.29 dev site with multi language setup.

This is to confirm your problem.

Looks like a bug to me. Maybe you want to file an issue at github.

Link to comment
Share on other sites

For me it works just fine "image.count>0" to show a textfield only if images has images. But not positive this was always the case with using Dependencies and subfields.

image_field != false
image_field.count = 0

Aren't valid selectors. It has whitespace.

image_field!=false
image_field.count=0

For image fields it only would make sense to use

image_field>0

  • Like 1
Link to comment
Share on other sites

Ok, images and files fields aren't supported. It's not a matter of the textfield being multilanguage or not, it's how file fields and dependencies work.

images.count>0 on a text field will only work if you upload a image via the dialog upload, cause that <input name="image[]"> is the one observed checking for its count. Once you save the image and reload the page that input is again 0. So it's not a bug but a "not supported" thing.

  • Like 4
Link to comment
Share on other sites

What is possible is to use a concat field to "save" the count of the images field and then use that for the visible dependency selector.

Create a concat field "imagescount" and enter "images.count" as the value.

Add that field to the template. And enter "imagescount>0" in the textfield's dependency.

This works after only after you uploaded images and saved the page.

  • Like 6
Link to comment
Share on other sites

Sorry for the spaces -- it was written for clarity, not actual use, as you probably figured out.  I was trying to hide and show checkboxes.

A concat field is an interesting solution, I might try that.  It would be neat if the selector could work for those fields, or if the field dependencies would allow an anonymous PHP or JS function that would return boolean.  Not the biggest deal in my context, though.  Thanks for the help!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...