You are here

function pmpapi_pull_admin_config_validate in Public Media Platform API Integration 7

Form validation handler for pmpapi_pull_admin_config().

See also

pmpapi_pull_admin_config_submit()

File

pmpapi_pull/pmpapi_pull.admin.inc, line 160
Basic admin forms, validators, and submit handlers.

Code

function pmpapi_pull_admin_config_validate($form, &$form_state) {
  $taken_profiles = array();
  foreach (pmpapi_pull_get_entities() as $entity_type => $entity) {
    $bundles = $entity['bundles'];
    foreach ($bundles as $bundle_name => $bundle) {
      $uname = $entity_type . '__' . $bundle_name;
      $profile = $form_state['values'][$uname . '_pull_profile'];
      if ($profile) {
        if (in_array($profile, $taken_profiles)) {
          form_set_error($uname . '_pull_profile', t('A profile can be mapped to no more than one content type.'));
        }
        $taken_profiles[] = $profile;
        $pmp_fields = pmpapi_get_profile_info($profile);
        $local_fields = pmpapi_get_augmented_fields($entity_type, $bundle_name);
        $taken_fields = array();
        foreach ($pmp_fields as $pmp_name => $pmp_field) {
          $value = $form_state['values']['pmpapi_pull_mapping_' . $uname . '_' . $pmp_name];
          if ($value !== '0') {
            if (!in_array($local_fields[$value]['type'], $pmp_field['accepted_types'])) {
              $error_msg = "To map from the PMP value {$pmp_name}, the (Drupal) field must be one of the following type(s): " . implode(', ', $pmp_field['accepted_types']);
              form_set_error('pmpapi_pull_mapping_' . $uname . '_' . $pmp_name, t('Incompatible field types.') . ' ' . $error_msg);
            }
            if (in_array($value, $taken_fields)) {
              form_set_error('pmpapi_pull_mapping_' . $uname . '_' . $pmp_name, t('Multiple PMP values cannot be mapped to the same (Drupal) field.'));
            }
            $taken_fields[] = $value;
          }
        }
      }
    }
  }
}