I am using a multi value image field which is set to hold 5 values. What I need, is to be able to set a lower cardinality for this field on specific nodes of the same content type.
Edit: Below is what I ended up using, it might help someone.
/** * Implements hook_field_attach_form(). */ function example_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) { // Our field name $field_name = 'field_listing_images'; // You may want to add more conditions if (isset($form[$field_name][LANGUAGE_NONE])) { // Our custom cardinality (hardcoded here) $custom_cardinality = 3; // Unset all field multi values that are equal or larger than our $custom_cardinality variable foreach ($form[$field_name][LANGUAGE_NONE] as $k => $value) { if ($k >= $custom_cardinality) { unset($form[$field_name][LANGUAGE_NONE][$k]); } } // Our custom form validation $form['#validate'][] = 'example_custom_form_validate'; } } /** * Our custom form validation callback. */ function example_custom_form_validate($form, &$form_state) { // Our field name $field_name = 'field_listing_images'; // You may want to add more conditions if (isset($form_state['values'][$field_name][LANGUAGE_NONE])) { // Our custom cardinality (hardcoded here) $custom_cardinality = 3; // Unset all field multi values that are equal or larger than our $custom_cardinality variable foreach ($form_state['values'][$field_name][LANGUAGE_NONE] as $k => $value) { if ($k >= $custom_cardinality) { unset($form_state['values'][$field_name][LANGUAGE_NONE][$k]); } } } }
Sponsored by SupremePR