0

I have a content type which consists four types of fields. checkbox, radio, select, select multiple

I'm using field_info_instances('node','bundle_name') in my custom module to get filed info for my content type. I need this info to use somewhere.

My question is, how can I distinguish the checkbox type and radio type.same problem for select and multi select also.

Here is the output enter image description here

0

A field has a cardinality property (meaning how many values it can have) and a type property (meaning what general sort of form element to use).

I say general because the field object itself does not dictate the exact widget. That is controlled by options_field_widget_form() (docs here) which takes the field object and works out whether to use checkboxes or radio buttons based on the cardinality.

So yes you are right - examining the field object won't tell you which type of form element will be used, but you can assume it from the cardinality (so long as no-one has overridden options_field_widget_form()).

Multiple cardinality gets checkboxes; single cardinality means you are only allowed one choice, hence a set of radio buttons. By the way 1 and -1 are not the only possible cardinalities. -1 means unlimited. A cardinality of, say, 3 will also get a set of checkboxes.

0

I got the solution, I've used field_info_field function, In the return array I found the difference in cardinality.

The cardinality for checkbox is -1 and for radio button is 1. Similarly for select is 1 and multi select is -1

I found it myself, is there any better solution than this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.