You are here

function nat_form_alter in Node Auto Term [NAT] 6

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

Implementation of hook_form_alter().

File

./nat.module, line 115
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 ($form['#id'] == 'node-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.
        // N.B. Free-tag vocabularies are unaffected by this.
        if (isset($form['#node']->nid)) {
          foreach ($form['#node']->nat as $tid => $term) {
            $nat_terms[$term->vid] = $tid;

            // Cull associated terms and their children from the taxonomy form.
            if (isset($form['taxonomy'])) {
              foreach ($form['taxonomy'] as $vid => $values) {
                if ($term->vid == $vid) {
                  $children = _nat_taxonomy_term_children($tid, $vid);
                  foreach ($values['#options'] as $id => $option) {

                    // Discount the -None- entry.
                    if (is_object($option)) {
                      $option_id = array_pop(array_keys($option->option));
                      if (in_array($option_id, $children)) {
                        unset($form['taxonomy'][$vid]['#options'][$id]);
                      }
                    }
                  }
                  break;
                }
              }
            }
          }
        }

        // Related terms.
        if (isset($config['related'][$type])) {
          $form['nat'] = array(
            '#type' => 'fieldset',
            '#title' => t('Term information'),
            '#tree' => TRUE,
            '#weight' => -2,
            '#collapsible' => TRUE,
            '#collapsed' => FALSE,
          );
          foreach ($associations as $vocabulary_id) {
            $vocabulary = taxonomy_vocabulary_load($vocabulary_id);
            if ($vocabulary->relations) {
              if (isset($nat_terms[$vocabulary_id])) {
                $default = array_keys(taxonomy_get_related($nat_terms[$vocabulary_id]));
                $exclude = array(
                  $nat_terms[$vocabulary_id],
                );
              }
              else {
                $default = $exclude = array();
              }
              $form['nat']['related'][$vocabulary_id] = _taxonomy_term_select(t('Related @terms', array(
                '@terms' => $vocabulary->name,
              )), 'relations', $default, $vocabulary_id, NULL, 1, t('<none>'), $exclude);
            }
          }

          // Synonyms: It is assumed that the synonyms for NAT terms in
          // different vocabularies are the same.
          if (!empty($nat_terms)) {
            $tid = array_pop($nat_terms);
            $default = implode("\n", taxonomy_get_synonyms($tid));
          }
          else {
            $default = '';
          }
          $form['nat']['synonyms'] = array(
            '#type' => 'textarea',
            '#title' => t('Synonyms'),
            '#default_value' => $default,
            '#description' => t('Synonyms of this term; one synonym per line.'),
          );
        }

        // This can only match once, so we just break the foreach.
        break;
      }
    }
  }
}