Преглед изворни кода

Added starting depth for selectable items

Signed-off-by: bachy <git@g-u-i.net>
bachy пре 13 година
родитељ
комит
f4fc36431c
1 измењених фајлова са 18 додато и 6 уклоњено
  1. 18 6
      term_reference_tree.widget.inc

+ 18 - 6
term_reference_tree.widget.inc

@@ -22,6 +22,7 @@ function term_reference_tree_field_widget_info() {
         'token_display' => '',
         'parent_term_id' => '',
         'max_depth' => '',
+        'starting_depth' => 1,
       ),
     ),
   );
@@ -181,6 +182,15 @@ function term_reference_tree_field_widget_settings_form($field, $instance) {
       '#return_value' => 1,
     );    
     
+    $form['starting_depth'] = array(
+      '#type' => 'textfield',
+      '#title' => t('Starting Depth'),
+      '#description' => t("Only items equal and down this level will be selectable. First level is 1"),
+      '#default_value' => $settings['starting_depth'],
+      '#size' => 2,
+      '#return_value' => 1,
+    );    
+
     $form['parent_term_id'] = array(
       '#type' => 'textfield',
       '#title' => t('Parent Term ID'),
@@ -300,9 +310,10 @@ function term_reference_tree_process_checkbox_tree($element, $form_state) {
     if ($max_choices != 1)
       $element['#tree'] = TRUE;
 
+		$starting_depth = !empty($element['#starting_depth']) ? $element['#starting_depth'] : 0;
     $tree = new stdClass;
     $tree->children = $terms;
-    $element[] = _term_reference_tree_build_level($element, $tree, $form_state, $value, $max_choices, array(), 1);
+    $element[] = _term_reference_tree_build_level($element, $tree, $form_state, $value, $max_choices, $starting_depth, array(), 1);
 
     //Add a track list element?
     $track_list = !empty($element['#track_list']) && $element['#track_list'];
@@ -506,6 +517,7 @@ function term_reference_tree_field_widget_form(&$form, &$form_state, $field, $in
       $element['#default_value'] = $multiple ? $default_value : array(reset($default_value) => reset($default_value));
       $element['#max_choices'] = $field['cardinality'];
       $element['#max_depth'] = $settings['max_depth'];
+      $element['#starting_depth'] = $settings['starting_depth'];
       $element['#start_minimized'] = $settings['start_minimized'];
       $element['#leaves_only'] = $settings['leaves_only'];
       $element['#filter_view'] = module_exists('views') ? $settings['filter_view'] : '';
@@ -649,7 +661,7 @@ function _term_reference_tree_get_allowed_values($filter) {
  *   A completed checkbox_tree_item element, which contains a checkbox and
  *   possibly a checkbox_tree_level element as well.
  */
-function _term_reference_tree_build_item(&$element, &$term, &$form_state, &$value, $max_choices, $parent_tids, $parent, $depth) {
+function _term_reference_tree_build_item(&$element, &$term, &$form_state, &$value, $max_choices, $starting_depth, $parent_tids, $parent, $depth) {
   $start_minimized = FALSE;
   if (array_key_exists('#start_minimized', $element)) {
     $start_minimized = $element['#start_minimized'];
@@ -676,7 +688,7 @@ function _term_reference_tree_build_item(&$element, &$term, &$form_state, &$valu
     '#depth' => $depth,
   );
 
-  if (!$element['#leaves_only'] || count($term->children) == 0) {
+  if ((!$element['#leaves_only'] || count($term->children) == 0) && $depth >= $element['#starting_depth']) {
     $name = "edit-" . str_replace('_', '-', $element['#field_name']);
     $e = array(
       '#type' => ($max_choices == 1) ? 'radio' : 'checkbox',
@@ -716,7 +728,7 @@ function _term_reference_tree_build_item(&$element, &$term, &$form_state, &$valu
   if (($depth + 1 <= $element['#max_depth'] || !$element['#max_depth']) && property_exists($term, 'children') && count($term->children) > 0) {
     $parents = $parent_tids;
     $parents[] = $term->tid;
-    $container[$term->tid . '-children'] = _term_reference_tree_build_level($element, $term, $form_state, $value, $max_choices, $parents, $depth+1);
+    $container[$term->tid . '-children'] = _term_reference_tree_build_level($element, $term, $form_state, $value, $max_choices, $starting_depth, $parents, $depth+1);
     $container['#level_start_minimized'] = $container[$term->tid . '-children']['#level_start_minimized'];
   }
   
@@ -744,7 +756,7 @@ function _term_reference_tree_build_item(&$element, &$term, &$form_state, &$valu
  * @return
  *   A completed checkbox_tree_level element.
  */
-function _term_reference_tree_build_level(&$element, &$term, &$form_state, &$value, $max_choices, $parent_tids, $depth) {
+function _term_reference_tree_build_level(&$element, &$term, &$form_state, &$value, $max_choices, $starting_depth, $parent_tids, $depth) {
   $start_minimized = FALSE;
   if (array_key_exists('#start_minimized', $element)) {
     $start_minimized = $element['#start_minimized'];
@@ -766,7 +778,7 @@ function _term_reference_tree_build_level(&$element, &$term, &$form_state, &$val
   $container['#level_start_minimized'] = $depth > 1 && $element['#start_minimized'] && !($term->children_selected);
   
   foreach($term->children as $t) {
-    $container[$t->tid] = _term_reference_tree_build_item($element, $t, $form_state, $value, $max_choices, $parent_tids, $container, $depth);
+    $container[$t->tid] = _term_reference_tree_build_item($element, $t, $form_state, $value, $max_choices, $starting_depth, $parent_tids, $container, $depth);
   }
   
   return $container;