Как сохранить и восстановить термины таксономии и дочерние термины в drupal? - PullRequest
0 голосов
/ 03 февраля 2019

Я успешно добавляю термины в конкретный словарь, используя пользовательский код, но когда я пытаюсь добавить новые дочерние термины для конкретного родителя, используя пользовательский код, он не отображается в разделе словаря, он отображается пустым на странице словаря.

Родитель таксономии

  • ребенок таксономии1
  • ребенок таксономии2
  • ребенок таксономии3

enter image description here

Мой код:

$from_voc = 12;
$to_voc = 20;
$tids = array('61377' => 61377);
$parents = array();
foreach ($tids as $tid) {

    $children = taxonomy_get_tree($from_voc, $tid);

    $terms_to_switch = array(); 
    $p=$tid;

    foreach ($children as $child) {
      $terms_to_switch[] = $child->tid;
      $term = taxonomy_term_load($child->tid);      
      $new_term = clone $term;
      $parent_id = trim($child->parents['0']);
      $parents[] =  $parent_id;
      unset($new_term->tid);
      $new_term->vid = $to_voc;
      taxonomy_term_save($new_term);     
    }  
    taxonomy_terms_static_reset();   

    //set parent of the selected term
    if (!count($parents)) {
      $parents[0] = 0;
    }
    taxonomy_update_voc($to_voc, $parents);
    taxonomy_terms_static_reset();
  }
function taxonomy_update_voc($vid, $parents = array()) {
  $voc = taxonomy_vocabulary_load($vid);
  if ($voc->vid == $vid) {
    $current_hierarchy = count($parents);
    if ($current_hierarchy > 2) {
      $current_hierarchy = 2;
      taxonomy_vocabulary_save($voc);
    }
    if ($current_hierarchy > $voc->hierarchy) {
      $voc->hierarchy = $current_hierarchy;
      taxonomy_vocabulary_save($voc);
    }
  }
}
...