1

I would like to limit the depth of a taxonomy vocabulary to a maximum number parent/child nestings. I would like my users to not be able to add at most 4 levels deep of taxonomy terms per tree. Is there a module for this or what code is needed to enforce this depth on the tree?

2 Answers 2

1

After a lot of Googling and trial and error I came up with the following means of limiting a vocab's depth. You can make it as specific as you like:

Limit the depth of Parents on the term edit page:

function MY_MODULE_form_taxonomy_form_term_alter(&$form, &$form_state) {
  if ($form['#bundle'] == 'MY_VOCAB') {
    // Limit parents to none or only top level items
    foreach ($form['relations']['parent']['#options'] as $tid => $option) {
      if (substr($option, 0, 1) == '-') {
        unset($form['relations']['parent']['#options'][ $tid ]);
      }
    }
  }
}

Limit the depth of Tabledrag on the taxonomy terms listing page:

I could not re-implement theme_taxonomy_overview_terms() as another module had already done this and it made no sense to do that.

function MY_MODULE_preprocess_page(&$variables) {
  if (current_path() == 'admin/structure/taxonomy/MY_VOCAB') {
    // Prevent tabledrag from allowing more than two levels in this hierarchy
    $settings['tableDrag']['taxonomy']['term-depth'][0] = array(
      'target' => 'term-depth',
      'source' => 'term-depth',
      'relationship' => 'group',
      'action' => 'depth',
      'hidden' => false,
      'limit' => 1,
    );
    drupal_add_js($settings, 'setting');
  }
}
0

No, there is no current way to limit a taxonomy depth using a 3rd party module that I know of. The maximum depth of taxonomies is limited by the database table structure, not just PHP code, to 9 levels in D7. You could add a custom validator on taxonomy term add/edit forms (and with extra logic certain Taxonomy Trees) to enforce a maximum parent depth. See Custom Validation while adding a term under taxonomy

4
  • Thanks tenken. Does this apply to the taxonomy itself? I'm not looking to limit what nodes can reference to, but the taxonomy itself. I don't want users to be able to create a fourth level, for example.
    – John
    Commented Jul 17, 2015 at 12:13
  • Applies to UI on Nodes. Not taxonomy admin UI.
    – tenken
    Commented Jul 17, 2015 at 12:47
  • Well, per my original question: I'm not talking about which terms a node can reference; instead I'm talking about actually limiting the number of levels a taxonomy vocabulary can contain. Also, what I said about the Taxonomy Manager UI was secondary, a plan B in case I couldn't limit the taxonomy otherwise.
    – John
    Commented Jul 17, 2015 at 16:27
  • please see my edits to simplify your question and my answer.
    – tenken
    Commented Jul 17, 2015 at 16:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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