2

I'm running into a problem of programmatically checking fields in a field collection.

What I want to do is loop through all fields within a field collection and check which ones are required.

I've tried these codes so far but haven't got any positive results.

$collections = field_collection_item_load_multiple($collection_ids);
foreach ($collections as $collection) {
   foreach($collection as $key => $va) {
        if (strpos ($key, 'field') !== false) {
            if (isset($va['und']) && is_array($va ['und']) && isset($va['und'][0]['value'])) {
                print "Field ".$key." ".$va['und'][0]['value'];
            }
        }
    }                   
}

This shows me the data and the label of the fields, not really their properties.

Thank you for your help.

1 Answer 1

0

Great Question :-) After re-engineering Field UI module found a simple way to retrieve above details...

  $bundle = field_extract_bundle('field_collection_item', 'field_my_collection_fields');

  $instances = field_info_instances('field_collection_item', $bundle);


  foreach ($instances as $field_name => $field_properties) {
   if ($field_properties['required'] == 1) {
     drupal_set_message($field_name . " is  required");
   }
   else {
     drupal_set_message($field_name . " is  not required");
   }
  }

Replace field_my_collection_fields with your field collection field name in above code ...

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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