- $parent = array_keys($taxonomy_storage->loadParents($term->id()));
- $children = $taxonomy_storage->loadTree($vocabulary->id(), $term->id());
-
- // A term can't be the child of itself, nor of its children.
- foreach ($children as $child) {
- $exclude[] = $child->tid;
+ $exclude = [];
+ if (!$term->isNew()) {
+ $parent = array_keys($taxonomy_storage->loadParents($term->id()));
+ $children = $taxonomy_storage->loadTree($vocabulary->id(), $term->id());
+
+ // A term can't be the child of itself, nor of its children.
+ foreach ($children as $child) {
+ $exclude[] = $child->tid;
+ }
+ $exclude[] = $term->id();