The taxonomy_default module does not provide default taxonomy terms for nodes already saved. This approach may be correct, in order to avoid that editing a node an unwanted term could be assigned to the node itself.
In my site I added a vocabulary to a node type, and made it "required" by configuration. I need to make sure that any time an user edits a node, the new default taxonomy term is selected if no term in that vocabulary has been previously assigned. I found out that such behaviour may be achieved with only minor changes in the form_alter() hook of the module.

Here is what I am proposing:

function taxonomy_defaults_form_alter($form_id, &$form) {
  // Only alter node forms
  if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id) {
    $node = $form['#node'];
    // Add the default 'pre-selected' terms to $node->taxonomy
    foreach (taxonomy_get_vocabularies($node->type) as $vid => $vocab) {
      if (variable_get("taxdef_{$node->type}_{$vid}_active", FALSE)) {
        // Do not preselect terms on nodes that already have been edited
        // except in case the term is required by configuration
        if (!isset($node->nid) || ($vocab->required && !taxonomy_node_get_terms_by_vocabulary($node->nid, $vid))) {
          $default_tids = variable_get("taxdef_{$node->type}_{$vid}", array());
          foreach ($default_tids as $default_tid) {
            $term = taxonomy_get_term($default_tid);
            $form['#node']->taxonomy[$default_tid] = $term;
          }
        }
      }
    }
  }
}

Comments

bradweikel’s picture

Version: 5.x-1.0 » master
Status: Active » Closed (won't fix)

I'm hesitant to change the default behavior, but would consider making this a configurable option if I get multiple requests (or if someone contributes the patch).

To clarify, these are the options I see:
1) Do nothing to old nodes (this is the current behavior)
2) Bulk update existing nodes when saving taxonomy_defaults, adding terms to nodes with no assigned terms for a given vocab
3) Bulk update, adding to all nodes regardless of existing terms
4) Update on node edit, if the node has no terms for that vocab (this is what pamatt proposes in this issue)
5) Update on node edit, regardless of existing terms assigned

#1 will remain the default behavior, no matter what, and I will only consider adding any of #2-5 if there is demand and a good UI.