You are here

function farm_group_field_farm_membership_validate in farmOS 7

Validation callback for the field_farm_group field in logs.

1 string reference to 'farm_group_field_farm_membership_validate'
farm_group_form_log_form_alter in modules/farm/farm_group/farm_group.module
Implements hook_form_FORM_ID_alter().

File

modules/farm/farm_group/farm_group.module, line 140

Code

function farm_group_field_farm_membership_validate($form, &$form_state) {

  // If there are no groups referenced in the log, bail.
  if (empty($form_state['values']['field_farm_membership'][LANGUAGE_NONE][0]['field_farm_group'][LANGUAGE_NONE])) {
    return;
  }

  // If there are no assets referenced in the log, warn the user.
  if (empty($form_state['values']['field_farm_asset'][LANGUAGE_NONE])) {
    form_set_error('field_farm_asset', t('No asset(s) have been selected to become members of the group(s). Please select asset(s) or remove the group(s).'));
  }

  // Build an array of asset IDs.
  $asset_ids = array();
  foreach ($form_state['values']['field_farm_asset'][LANGUAGE_NONE] as $reference) {
    if (!empty($reference['target_id'])) {
      $asset_ids[] = $reference['target_id'];
    }
  }

  // Build an array of group IDs.
  $group_ids = array();
  foreach ($form_state['values']['field_farm_membership'][LANGUAGE_NONE][0]['field_farm_group'][LANGUAGE_NONE] as $reference) {
    if (!empty($reference['target_id'])) {
      $group_ids[] = $reference['target_id'];
    }
  }

  // Validate the asset IDs and group IDs to prevent circular memberships.
  // If an issue is found, flag the 'field_farm_asset' element in the form.
  farm_group_circular_membership_validate($asset_ids, $group_ids, 'field_farm_asset');
}