function nat_fields_form_validate in Node Auto Term [NAT] 7
Same name and namespace in other branches
- 7.2 nat.admin.inc \nat_fields_form_validate()
Validate NAT fields form submissions.
File
- ./
nat.admin.inc, line 141 - 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();
foreach ($form_values['nat'] as $node_type => $vocabulary) {
foreach ($vocabulary as $vid => $fields) {
$fieldset = $all_types[$node_type]->name . '<->' . $all_vocabularies[$vid];
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][$vid] = TRUE;
}
// We assume that the fields are in the order term => node.
$associations[$node_type][$vid][$field['term']] = $field['node'];
}
else {
// form_set_error does not like fields which are any more specific.
form_set_error('nat_' . $node_type . '-' . $vid, 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][$vid]) || count($associations[$node_type][$vid]) == 0) {
form_set_error('nat_' . $node_type . '-' . $vid, 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 $vid => $fields) {
if (!isset($term_title_check[$node_type][$vid])) {
form_set_error('nat_' . $node_type . '-' . $vid, 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);
}