|
|
|
@@ -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();
|
|
|
|
|
// 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 ($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));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 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'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 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']));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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 ($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);
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|