Sfoglia il codice sorgente

debug

ordre non respect (js compare delatas as string and not numbers)
term disparaissant (car delta identiques)
translate (entity translate) delete terms

Signed-off-by: bachy <git@g-u-i.net>
bachy 13 anni fa
parent
commit
4cf9d7b2c6
2 ha cambiato i file con 95 aggiunte e 68 eliminazioni
  1. 5 3
      term_reference_tree.js
  2. 90 65
      term_reference_tree.widget.inc

+ 5 - 3
term_reference_tree.js

@@ -246,10 +246,14 @@
 				if(delta == -1){
 					track_list_container.append(new_item);				
 					inserted_flag = true;
+					$hiddenInput = $('#'+control_id).parent('.form-item').next('input[type=hidden]');
+					// console.log('$hiddenInput',$hiddenInput);
+					$hiddenInput.val(new_item.index());
+					
 				}else{
 					list_items.each(function(index){
 			      item_comparing_to = $(this);
-						if ( delta < item_comparing_to.attr('delta') ) {
+						if ( parseInt(delta) < parseInt(item_comparing_to.attr('delta')) ) {
 			        //Add it here.
 			        item_comparing_to.before(new_item);
 			        inserted_flag = true;
@@ -262,8 +266,6 @@
 		    
 				}
 				
-				$hiddenInput = $('#'+control_id).parent('.form-item').next('input[type=hidden]');
-				$hiddenInput.val(new_item.index());
 				
 				track_list_container.sortable('refresh');
 			}

+ 90 - 65
term_reference_tree.widget.inc

@@ -587,75 +587,100 @@ function term_reference_tree_field_widget_form(&$form, &$form_state, $field, $in
  *   The validated element.
  */
 function _term_reference_tree_widget_validate(&$element, &$form_state) {
-  $items = _term_reference_tree_flatten($element, $form_state);
-  $value = array();
-
-  if ($element['#max_choices'] != 1) {
-		if(!$element['#track_list_order']){
-	    foreach($items as $child) {
-	      if (array_key_exists('#value', $child) && $child['#value'] !== 0) {
-	        array_push($value, array( $element['#value_key'] => $child['#value']));
-
-	        // If the element is leaves only and select parents is on, then automatically
-	        // add all the parents of each selected value.
-	        if ($element['#select_parents'] && $element['#leaves_only']) {
-	          foreach($child['#parent_values'] as $parent_tid) {
-	            if (!in_array(array($element['#value_key'] => $parent_tid), $value)) {
-	              array_push($value, array($element['#value_key'] => $parent_tid));
-	            }
-	          }
-	        }
-
-	      } 
-	    }
+	// dsm($element, '$element');
+	// dsm($form_state, '$form_state');
+	# if the field was in the form
+	if(isset($form_state['input'][$element['#field_name']])){
+		
+	  $items = _term_reference_tree_flatten($element, $form_state);
+	  $value = array();
+
+	  if ($element['#max_choices'] != 1) {
+			if(!$element['#track_list_order']){
+		    foreach($items as $child) {
+		      if (array_key_exists('#value', $child) && $child['#value'] !== 0) {
+		        array_push($value, array( $element['#value_key'] => $child['#value']));
+
+		        // If the element is leaves only and select parents is on, then automatically
+		        // add all the parents of each selected value.
+		        if ($element['#select_parents'] && $element['#leaves_only']) {
+		          foreach($child['#parent_values'] as $parent_tid) {
+		            if (!in_array(array($element['#value_key'] => $parent_tid), $value)) {
+		              array_push($value, array($element['#value_key'] => $parent_tid));
+		            }
+		          }
+		        }
+
+		      } 
+		    }
 			
-		}else{
-			$selected_terms = array();			
-			foreach($items as $child) {
-	      if (array_key_exists('#value', $child) && $child['#value'] !== 0) {
-					$delta = $form_state['input'][$child['#value'].'-weight'];
-	        $selected_terms[$delta] = array($element['#value_key'] => $child['#value']);
+			}else{
+				$selected_terms = array();			
+				$deltas = array();
+				foreach($items as $child) {
+		      if (array_key_exists('#value', $child) && $child['#value'] !== 0) {
+		        $selected_terms[] = array(
+							"delta" => $form_state['input'][$child['#value'].'-weight'],
+							"term" => array($element['#value_key'] => $child['#value']),
+						);
 				
-	        // If the element is leaves only and select parents is on, then automatically
-	        // add all the parents of each selected value.
-	        if ($element['#select_parents'] && $element['#leaves_only']) {
-	          foreach($child['#parent_values'] as $parent_tid) {
-	            if (!in_array(array($element['#value_key'] => $parent_tid), $selected_terms)) {
-								$delta = $form_state['input'][$parent_tid.'-weight'];
-				        $selected_terms[$delta] = array($element['#value_key'] => $parent_tid);
-	            }
-	          }
-	        }
-
-	      } 
-	    }
-			// reorder items
-			ksort($selected_terms);
-			// record in value
-			foreach ($selected_terms as $delta => $term) {
-				$value[] = $term;
-			}
-
-		}
-  }
-  else {
-    // If it's a tree of radio buttons, they all have the same value, so we can just
-    // grab the value of the first one.
-    if (count($items) > 0) {
-      $child = reset($items);
-      if (array_key_exists('#value', $child) && $child['#value'] !== 0) {
-        array_push($value, array($element['#value_key'] => $child['#value']));
-      }       
-    }
-  }
+		        // If the element is leaves only and select parents is on, then automatically
+		        // add all the parents of each selected value.
+		        if ($element['#select_parents'] && $element['#leaves_only']) {
+		          foreach($child['#parent_values'] as $parent_tid) {
+		            if (!in_array(array($element['#value_key'] => $parent_tid), $selected_terms)) {
+					        $selected_terms[] = array(
+										"delta" => $form_state['input'][$parent_tid.'-weight'],
+										"term" => array($element['#value_key'] => $child['#value']),
+									);
+
+		            }
+		          }
+		        }
+
+		      } 
+		    }
+				// dsm($deltas, '$deltas');
+				// dsm($selected_terms, '$selected_terms before sort');
+				// reorder items
+				usort($selected_terms, function($a, $b){
+					return $a['delta'] > $b['delta'];
+				});
+				dsm($selected_terms, '$selected_terms after sort');
+				
+				// record in value
+				foreach ($selected_terms as $selected_term) {
+					$value[] = $selected_term['term'];
+				}
 
-  if ($element['#required'] && empty($value)) {
-    // The title is already check_plained so it's appropriate to use !.
-    form_error($element, t('!name field is required.', array('!name' => $element['#title'])));
-  }
+			}
+	  }
+	  else {
+	    // If it's a tree of radio buttons, they all have the same value, so we can just
+	    // grab the value of the first one.
+	    if (count($items) > 0) {
+	      $child = reset($items);
+	      if (array_key_exists('#value', $child) && $child['#value'] !== 0) {
+	        array_push($value, array($element['#value_key'] => $child['#value']));
+	      }       
+	    }
+	  }
 
-  form_set_value($element, $value, $form_state);
-  return $element;
+	  if ($element['#required'] && empty($value)) {
+	    // The title is already check_plained so it's appropriate to use !.
+	    form_error($element, t('!name field is required.', array('!name' => $element['#title'])));
+	  }
+	
+	  form_set_value($element, $value, $form_state);
+		// dsm($element, '$element afetr form_set_value');
+	  return $element;
+	}else{
+		# if the field was not in the form
+	  form_set_value($element, $element['#default_value'], $form_state);
+		// dsm($element, '$element afetr form_set_value');
+	  return $element;
+		
+	}
 }
 
 /**