You are here

function nat_form_alter in Node Auto Term [NAT] 7.2

Same name and namespace in other branches
  1. 5 nat.module \nat_form_alter()
  2. 6.2 nat.module \nat_form_alter()
  3. 6 nat.module \nat_form_alter()
  4. 7 nat.module \nat_form_alter()

Implements hook_form_alter().

File

./nat.module, line 168
NAT - node auto term - is a helper module that automatically creates a term using the same title as a node.

Code

function nat_form_alter(&$form, &$form_state, $form_id) {
  if (isset($form['#node_edit_form'])) {
    $config = _nat_variable_get();
    foreach ($config['types'] as $type => $associations) {
      if (count($associations) && $form_id == $type . '_node_form') {
        $nat_terms = array();

        // If this is a node update, remove this node's associated terms from
        // its associated vocabularies.
        if (isset($form['#node']->nid)) {
          $vocabularies = _nat_node_form_vocabularies_get($form);

          // Cull associated terms and their children from the taxonomy form.
          foreach ($vocabularies as $vid => $vocabulary) {
            foreach ($form['#node']->nat as $tid => $term) {
              $nat_terms[$term->vid] = $tid;
              if ($term->vid == $vid) {
                $children = _nat_taxonomy_term_children($tid, $vid);

                // A vocabulary might be associated with multiple form fields.
                foreach ($vocabulary->form_fields as $form_field) {

                  // Apparently, terms are not translatable in D7. Nevertheless,
                  // going with the flow as options are collected under the
                  // 'und' heading.
                  $language = $form[$form_field]['#language'];
                  if (isset($form[$form_field][$language]['#options'])) {
                    $form[$form_field][$language]['#options'] = array_diff_key($form[$form_field][$language]['#options'], $children);
                  }
                }
              }
            }
          }
          $form['nat_vocabularies'] = array(
            '#type' => 'value',
            '#value' => $vocabularies,
          );
        }
      }
    }
  }
}