1

I want to place a new text field in the "Vocabularies" field set of a node; the text field is not tag.

I am using the Vertical Tabs module, and my fields in the content type are in group.

The following is the structure of my content type:

  • Title
  • Vocabularies (taxonomy) -> Here I want to place a field too.
  • fieldset1 (Fields: field1, field2, and field3)
  • fieldset2 (Fields: field4, field5)
0

The fieldset for the vocabularies used by a node are added from taxonomy_form_alter(), using the following code (the relevant part is the one between "// 3" and "// 4"):

// 1
if (!isset($node->taxonomy)) {
  $terms = empty($node->nid) ? array() : taxonomy_node_get_terms($node);
}
else {
  // After a preview or form reload, the terms must be converted to objects.
  reset($node->taxonomy);
  if (!is_object(current($node->taxonomy))) {
    $node->taxonomy = taxonomy_preview_terms($node);
  }
  $terms = $node->taxonomy;
}

$c = db_query(db_rewrite_sql("SELECT v.* FROM {vocabulary} v INNER JOIN {vocabulary_node_types} n ON v.vid = n.vid WHERE n.type = '%s' ORDER BY v.weight, v.name", 'v', 'vid'), $node->type);

while ($vocabulary = db_fetch_object($c)) {
  if ($vocabulary->tags) {
    if (isset($form_state['node_preview'])) {
      // Typed string can be changed by the user before preview,
      // so we just insert the tags directly as provided in the form.
      $typed_string = $node->taxonomy['tags'][$vocabulary->vid];
    }
    else {
      $typed_string = taxonomy_implode_tags($terms, $vocabulary->vid) . (array_key_exists('tags', $terms) ? $terms['tags'][$vocabulary->vid] : NULL);
    }
// 2
    if ($vocabulary->help) {
      $help = filter_xss_admin($vocabulary->help);
    }
    else {
      $help = t('A comma-separated list of terms describing this content. Example: funny, bungee jumping, "Company, Inc.".');
    }
    $form['taxonomy']['tags'][$vocabulary->vid] = array(
      '#type' => 'textfield', 
      '#title' => $vocabulary->name, 
      '#description' => $help, 
      '#required' => $vocabulary->required, 
      '#default_value' => $typed_string, 
      '#autocomplete_path' => 'taxonomy/autocomplete/' . $vocabulary->vid, 
      '#weight' => $vocabulary->weight, 
      '#maxlength' => 1024,
    );
  }
  else {
    // Extract terms belonging to the vocabulary in question.
    $default_terms = array();
    foreach ($terms as $term) {
      // Free tagging has no default terms and also no vid after preview.
      if (isset($term->vid) && $term->vid == $vocabulary->vid) {
        $default_terms[$term->tid] = $term;
      }
    }

// 3
    $form['taxonomy'][$vocabulary->vid] = taxonomy_form($vocabulary->vid, array_keys($default_terms), filter_xss_admin($vocabulary->help));
    $form['taxonomy'][$vocabulary->vid]['#weight'] = $vocabulary->weight;
    $form['taxonomy'][$vocabulary->vid]['#required'] = $vocabulary->required;
  }
}
if (!empty($form['taxonomy']) && is_array($form['taxonomy'])) {
  if (count($form['taxonomy']) > 1) {
    // Add fieldset only if form has more than 1 element.
    $form['taxonomy'] += array(
      '#type' => 'fieldset', 
      '#title' => t('Vocabularies'), 
      '#collapsible' => TRUE, 
      '#collapsed' => FALSE,
    );
  }
  $form['taxonomy']['#weight'] = -3;
  $form['taxonomy']['#tree'] = TRUE;
// 4
}

The fieldset, which is then changed by the Vertical Tabs module in a tab, is added only when there are more than one vocabulary associated with the content type of the node being created.

To add a new field in that fieldset, you should implement hook_form_alter() in a module, and add code similar to the following one:

function mymodule_form_alter(&$form, $form_state, $form_id) {
  if (isset($form['type']) && isset($form['#node']) && && $form['type']['#value'] . '_node_form' == $form_id) {
    if (isset($form['taxonomy']['#type']) && $form['taxonomy']['#type'] == 'fieldset') {
      // Add your field.
      $form['taxonomy']['mymodule_field] = array(
        // …
      );
    }
  }
}

The module you write should be executed after the Taxonomy module, or the module will not find the fieldset added by the Taxonomy module. The module is executed after the Taxonomy module if it has a name such as TaxonomyAddendum, or if the weight of the module is higher than the weight of the Taxonomy module (10 should be enough).

As for altering the existing fields for the taxonomy terms, you should look at the code between the comments "// 1" and "// 2"; as form is build using the vocabulary ID, as in $form['taxonomy'][$vocabulary->vid], you need to know it to get all the form fields used for the taxonomy terms. If you want to change the form field for a vocabulary for which you know the ID, then the code is simpler, as you don't need to get all the vocabularies that are associated to the node being edited; that is also true if you want to alter the form field for a vocabulary you know by name. You can use the following code to get a vocabulary object knowing the vocabulary name.

$result = db_fetch_object(db_query(db_rewrite_sql("SELECT v.vid, v.*, n.type FROM {vocabulary} v LEFT JOIN {vocabulary_node_types} n ON v.vid = n.vid WHERE n.type = '%s' AND v.name = '%s' ORDER BY v.weight, v.name", 'v', 'vid'), $type, $vocabulary_name));

$type is the content type that, in your case, is $form['#node']->type; $vocabulary_name is the name of the vocabulary.

If you know the vocabulary ID, then you can simply use taxonomy_vocabulary_load() to get the vocabulary object, which you could need to populate the taxonomy term form field.

|improve this answer|||||
  • Thank for your answer. I see that this code works for adding a new field in the section vocabularies. How can I stores the information of a new field? or Do you know how can I place an existent field in the tab vocabularies in a content type? I prefer this last option. Thanks. – cabita Aug 14 '11 at 22:50
  • I added that information too. – kiamlaluno Aug 15 '11 at 0:05
  • Thanks for your time and your answer. This answer is useful for add a new field in the tab vocabularies. I have added other question about the move an existent field into tab vocabularies. Thanks again. – cabita Aug 15 '11 at 0:32
  • I forgot to put the link to the new question: drupal.stackexchange.com/questions/9270/… Thanks. – cabita Aug 15 '11 at 1:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.