diff --git a/src/Controller/HmTaxonomyController.php b/src/Controller/HmTaxonomyController.php index f9c35d40a5968762e036fe523b4baa37479d677d..e0992b1370923d25e9d02e02d2219d0e90757e0b 100644 --- a/src/Controller/HmTaxonomyController.php +++ b/src/Controller/HmTaxonomyController.php @@ -164,39 +164,49 @@ class HmTaxonomyController extends ControllerBase { // Children of the parent term in weight and name alphabetically order. $children = $this->storageController->loadTree($vid, $parent_id, 1); 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']); + } } - - $target_position = intval($target_position); - $total = count($children); - // Move all terms after the target position forward. - if (isset($children[$target_position])) { - $weight = (int) $children[$target_position]->weight; - $tids = []; - $step = $weight + count($updated_terms); - for ($i = $target_position; $i < $total; $i++) { - if ($children[$i]->weight < $step++) { - $tids[] = $children[$i]->tid; + else { + // The parent term has children. + $target_position = intval($target_position); + $total = count($children); + // Move all terms after the target position forward. + if (isset($children[$target_position])) { + $weight = (int) $children[$target_position]->weight; + $tids = []; + $step = $weight + count($updated_terms); + for ($i = $target_position; $i < $total; $i++) { + if ($children[$i]->weight < $step++) { + $tids[] = $children[$i]->tid; + } + else { + // There is planty room, no need to move anymore. + break; + } } - else { - // There is planty room, no need to move anymore. - break; + $step = $weight + count($updated_terms); + $term_siblings = Term::loadMultiple($tids); + foreach ($term_siblings as $term) { + $term->setWeight($step++); + $success = $term->save(); } } - $step = $weight + count($updated_terms); - $term_siblings = Term::loadMultiple($tids); - foreach ($term_siblings as $term) { - $term->setWeight($step++); - $success = $term->save(); + 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.']); } } - 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. $terms = Term::loadMultiple($updated_terms); // Update all terms, the weight will be increased by 1, diff --git a/src/Plugin/HmDisplayPlugin/HmDisplayJstree.php b/src/Plugin/HmDisplayPlugin/HmDisplayJstree.php index 107d44b6b321467a36dd81956d86ee9f30e8d2e5..c5588bac45800235050b67b09ec38f88d5845c04 100644 --- a/src/Plugin/HmDisplayPlugin/HmDisplayJstree.php +++ b/src/Plugin/HmDisplayPlugin/HmDisplayJstree.php @@ -51,7 +51,7 @@ class HmDisplayJstree extends HmDisplayPluginBase implements HmDisplayPluginInte $form['jstree'] = [ '#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', '#value' => '', '#attributes' => [