Skip to content
Snippets Groups Projects
Commit bc335d23 authored by Mingsong Hu's avatar Mingsong Hu
Browse files

Fix non-parent issue

parent 0ebaeaa0
No related branches found
No related tags found
No related merge requests found
...@@ -164,39 +164,49 @@ class HmTaxonomyController extends ControllerBase { ...@@ -164,39 +164,49 @@ class HmTaxonomyController extends ControllerBase {
// Children of the parent term in weight and name alphabetically order. // Children of the parent term in weight and name alphabetically order.
$children = $this->storageController->loadTree($vid, $parent_id, 1); $children = $this->storageController->loadTree($vid, $parent_id, 1);
if (empty($children)) { if (empty($children)) {
return new JsonResponse(['result' => 'fail']); if (Term::load($parent_id)) {
// The parent term hasn't had any children.
$weight = 0;
}
else {
// The parent term doesn't exist.
return new JsonResponse(['result' => 'fail']);
}
} }
else {
$target_position = intval($target_position); // The parent term has children.
$total = count($children); $target_position = intval($target_position);
// Move all terms after the target position forward. $total = count($children);
if (isset($children[$target_position])) { // Move all terms after the target position forward.
$weight = (int) $children[$target_position]->weight; if (isset($children[$target_position])) {
$tids = []; $weight = (int) $children[$target_position]->weight;
$step = $weight + count($updated_terms); $tids = [];
for ($i = $target_position; $i < $total; $i++) { $step = $weight + count($updated_terms);
if ($children[$i]->weight < $step++) { for ($i = $target_position; $i < $total; $i++) {
$tids[] = $children[$i]->tid; if ($children[$i]->weight < $step++) {
$tids[] = $children[$i]->tid;
}
else {
// There is planty room, no need to move anymore.
break;
}
} }
else { $step = $weight + count($updated_terms);
// There is planty room, no need to move anymore. $term_siblings = Term::loadMultiple($tids);
break; foreach ($term_siblings as $term) {
$term->setWeight($step++);
$success = $term->save();
} }
} }
$step = $weight + count($updated_terms); elseif ($target_position === $total) {
$term_siblings = Term::loadMultiple($tids); // Insert into the end.
foreach ($term_siblings as $term) { $weight = intval(array_slice($children, -1)[0]->weight) + 1;
$term->setWeight($step++); }
$success = $term->save(); else {
return new JsonResponse(['result' => 'The term is not found.']);
} }
} }
elseif ($target_position === $total) {
// Insert into the end.
$weight = intval(array_slice($children, -1)[0]->weight) + 1;
}
else {
return new JsonResponse(['result' => 'The term is not found.']);
}
// Load all terms needed to update. // Load all terms needed to update.
$terms = Term::loadMultiple($updated_terms); $terms = Term::loadMultiple($updated_terms);
// Update all terms, the weight will be increased by 1, // Update all terms, the weight will be increased by 1,
......
...@@ -51,7 +51,7 @@ class HmDisplayJstree extends HmDisplayPluginBase implements HmDisplayPluginInte ...@@ -51,7 +51,7 @@ class HmDisplayJstree extends HmDisplayPluginBase implements HmDisplayPluginInte
$form['jstree'] = [ $form['jstree'] = [
'#type' => 'html_tag', '#type' => 'html_tag',
'#suffix' => '<div class="description">' . $this->t('You can double click a tree node to edit it.') . '<br>' . $this->t('The tree node is draggable and droppable') . '</div>', '#suffix' => '<div class="description">' . $this->t('Click a tree node to edit it.') . '<br>' . $this->t('The tree node is draggable and droppable') . '</div>',
'#tag' => 'div', '#tag' => 'div',
'#value' => '', '#value' => '',
'#attributes' => [ '#attributes' => [
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment