You are here

function nat_fields_form in Node Auto Term [NAT] 7

Same name and namespace in other branches
  1. 7.2 nat.admin.inc \nat_fields_form()

Menu callback: NAT module fields form. @todo This form is something of a mess and could use a little clean-up.

1 string reference to 'nat_fields_form'
nat_menu in ./nat.module
Implements hook_menu().

File

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

Code

function nat_fields_form() {
  $nat_config = _nat_variable_get();
  $all_types = node_type_get_types();
  $all_vocabularies = _nat_get_vocabularies();
  $form['nat_field_config'] = array(
    '#type' => 'vertical_tabs',
  );
  $header = array(
    'node' => array(
      'data' => t('Node field'),
    ),
    'term' => array(
      'data' => t('Term field'),
    ),
  );
  foreach ($nat_config['types'] as $type => $vocabularies) {
    $node_fields = array_map('_nat_field_label', field_info_instances('node', $type));
    $node_fields = array_filter($node_fields);
    $node_fields = array(
      '0' => t('--None--'),
      'title' => t('Title (inbuilt)'),
    ) + $node_fields;
    foreach ($vocabularies as $vid) {
      $vocabulary = taxonomy_vocabulary_load($vid);
      $term_fields = array_map('_nat_field_label', field_info_instances('taxonomy_term', $vocabulary->machine_name));
      $term_fields = array(
        '0' => t('--None--'),
        'name' => t('Name (inbuilt)'),
        'description' => t('Description (inbuilt)'),
      ) + $term_fields;
      $form_field_name = "nat_{$type}-{$vid}";
      $form[$form_field_name] = array(
        '#title' => check_plain($all_types[$type]->name . '<->' . $all_vocabularies[$vid]),
        '#type' => 'fieldset',
        '#description' => t('Match fields from the node form to their corresponding fields in the taxonomy term form.'),
        '#group' => 'nat_field_config',
      );
      $rows = array();
      $i = 0;
      foreach ($term_fields as $name => $field) {
        if ($name != '0') {
          $association_exists = isset($nat_config['associations'][$type]) && isset($nat_config['associations'][$type][$vid]);
          $association = $association_exists && isset($nat_config['associations'][$type][$vid][$name]) ? $name : NULL;
          $rows[] = array(
            'node' => array(
              '#type' => 'select',
              '#options' => $node_fields,
              '#parents' => array(
                'nat',
                $type,
                $vid,
                $i,
                'node',
              ),
              '#default_value' => isset($association) ? array(
                $nat_config['associations'][$type][$vid][$name],
              ) : array(),
            ),
            'term' => array(
              '#type' => 'select',
              '#options' => $term_fields,
              '#parents' => array(
                'nat',
                $type,
                $vid,
                $i++,
                'term',
              ),
              '#default_value' => isset($association) ? array(
                $name,
              ) : array(),
            ),
          );
        }
      }
      $form[$form_field_name]['table'] = array(
        '#theme' => 'nat_table',
        'header' => array(
          '#type' => 'value',
          '#value' => $header,
        ),
        'rows' => $rows,
      );
    }
  }
  $form['associations'] = array(
    '#type' => 'value',
    '#value' => array(),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save field configuration'),
  );
  return $form;
}