You are here

function nat_fields_form_validate in Node Auto Term [NAT] 7.2

Same name and namespace in other branches
  1. 7 nat.admin.inc \nat_fields_form_validate()

Validate NAT fields form submissions.

File

./nat.admin.inc, line 145
NAT module administrative forms.

Code

function nat_fields_form_validate($form, &$form_state) {
  $form_values = $form_state['values'];
  $all_types = node_type_get_types();
  $all_vocabularies = _nat_get_vocabularies();
  $associations = $term_title_check = array();
  if (isset($form_values['nat'])) {
    foreach ($form_values['nat'] as $node_type => $vocabulary) {
      foreach ($vocabulary as $machine_name => $fields) {
        $fieldset = $all_types[$node_type]->name . '<->' . $machine_name;
        foreach ($fields as $id => $field) {

          // Remove unassociated fields.
          $field = array_filter($field);

          // The title, name and description fields are inbuilt fields.
          if (count($field) > 1) {
            if ($field['node'] == 'title') {
              $field_type_node = 'text';
            }
            else {
              $field_info_node = field_info_field($field['node']);
              $field_type_node = $field_info_node['type'];
            }
            if ($field['term'] == 'name' || $field['term'] == 'description') {
              $field_type_term = 'text';
            }
            else {
              $field_info_term = field_info_field($field['term']);
              $field_type_term = $field_info_term['type'];
            }

            // Check if the fields are compatible.
            if (_nat_field_types_match($field_type_node, $field_type_term)) {

              // The term title field is pretty much the only required field.
              if ($field['term'] == 'name') {
                $term_title_check[$node_type][$machine_name] = TRUE;
              }

              // We assume that the fields are in the order term => node.
              $associations[$node_type][$machine_name][$field['term']] = $field['node'];
            }
            else {

              // form_set_error does not like fields which are any more specific.
              form_set_error('nat_' . $node_type . '-' . $machine_name, t('[%fieldset] The field types of the node field, %field_node, and the term field, %field_term, do not match', array(
                '%fieldset' => $fieldset,
                '%field_node' => $field['node'],
                '%field_term' => $field['term'],
              )));
            }
          }
        }
        if (!isset($associations[$node_type]) || !isset($associations[$node_type][$machine_name]) || count($associations[$node_type][$machine_name]) == 0) {
          form_set_error('nat_' . $node_type . '-' . $machine_name, t('Each node-term association should have at least one field association.'));
        }
      }
    }
  }

  // Check for the presence of the title field in each association.
  foreach ($associations as $node_type => $vocabularies) {
    foreach ($vocabularies as $machine_name => $fields) {
      if (!isset($term_title_check[$node_type][$machine_name])) {
        form_set_error('nat_' . $node_type . '-' . $machine_name, t('At least one of the term fields associated with the %type node type has to be the title field.', array(
          '%type' => $node_type,
        )));
      }
    }
  }
  form_set_value($form['associations'], $associations, $form_state);
}