summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Hutterer2011-06-22 09:02:14 (GMT)
committer Matthias Hutterer2011-06-22 09:02:14 (GMT)
commitf62521bccbb5097e015c123edd06a1341583fdb8 (patch)
tree93cbee13b168a0b0d23d98974d99cc794850238e
parentc7277bc260c0bafada9819e8a468572369c10df9 (diff)
added option for the tree depth7.x-1.0-beta1
-rw-r--r--content_taxonomy.module46
1 files changed, 46 insertions, 0 deletions
diff --git a/content_taxonomy.module b/content_taxonomy.module
index 7136659..2b3db2b 100644
--- a/content_taxonomy.module
+++ b/content_taxonomy.module
@@ -27,6 +27,52 @@ function content_taxonomy_form_field_ui_field_edit_form_alter(&$form, &$form_sta
'#options' => $options,
'#default_value' => isset($tree['parent']) ? $tree['parent'] : 0,
);
+
+ $form['field']['settings']['allowed_values'][$delta]['depth'] = array(
+ '#type' => 'textfield',
+ '#title' => t('Tree depth'),
+ '#default_value' => isset($tree['depth']) ? $tree['depth'] : '',
+ '#description' => t('Set the depth of the tree. Leave empty to load all terms.'),
+ '#element_validate' => array('_element_validate_integer_positive'),
+ );
}
}
+}
+
+
+/**
+ * Implements hook_field_info_alter().
+ */
+function content_taxonomy_field_info_alter(&$info) {
+ // Use own options callback for handling additional configuration options.
+ $info['taxonomy_term_reference']['settings']['options_list_callback'] = 'content_taxonomy_allowed_values';
+
+ // Add depth option.
+ foreach ($info['taxonomy_term_reference']['settings']['allowed_values'] as $key => $values) {
+ $info['taxonomy_term_reference']['settings']['allowed_values'][$key]['depth'] = 0;
+ }
+}
+
+/**
+ * Returns the set of valid terms for a taxonomy field.
+ * Extends taxonomy_allowed_values() with the tree depth option.
+ *
+ * @param $field
+ * The field definition.
+ * @return
+ * The array of valid terms for this field, keyed by term id.
+ */
+function content_taxonomy_allowed_values($field) {
+ $options = array();
+ foreach ($field['settings']['allowed_values'] as $tree) {
+ if ($vocabulary = taxonomy_vocabulary_machine_name_load($tree['vocabulary'])) {
+ $max_depth = (isset($tree['depth']) && !empty($tree['depth'])) ? $tree['depth'] : NULL;
+ if ($terms = taxonomy_get_tree($vocabulary->vid, $tree['parent'], $max_depth)) {
+ foreach ($terms as $term) {
+ $options[$term->tid] = str_repeat('-', $term->depth) . $term->name;
+ }
+ }
+ }
+ }
+ return $options;
} \ No newline at end of file