During migration, parent-child relationship gets lost for some terms. This developer tip will help you to resolve the issue post-migration in Taxonomy. Also, It is expected that migrate module is used for the term migration.
$relation_ship_info = array('source_table' => 'ismaili_tag', // Name of source table for migration
'map_table' => 'migrate_map_geographyterm', // migrate generated table
'parent_field_name' => 'parenttagid', // name of parent field in source table
'source_map_field' => 'tagid' // name of field used in migration
);
MYMODULE_handle_broken_relationship($relation_ship_info);
/**
* Process Content Category Term names
* @param [type] $relation_ship_info Holds all information for maintaing broken parent-child
* relationship.
* @return [type] [description]
*/
function MYMODULE_handle_broken_relationship($relation_ship_info) {
$query = "SELECT mc.sourceid1, mc.destid1, cs." . $relation_ship_info['parent_field_name'] ." FROM " . $relation_ship_info['source_table'] ." cs
left join " . $relation_ship_info['map_table'] ." mc ON (cs." . $relation_ship_info['source_map_field'] ." = mc.sourceid1)
left join taxonomy_term_hierarchy th ON (mc.destid1 = th.tid)
WHERE th.parent = 0 and cs." . $relation_ship_info['parent_field_name'] . " > 0";
$result = db_query($query);
$missing_tid_parentid = '';
foreach($result as $data) {
$missing_content_data[$data->sourceid1] = $data;
$missing_tid_parentid = ($missing_tid_parentid != '') ? $missing_tid_parentid . "," .$data->{$relation_ship_info['parent_field_name']} : $data->{$relation_ship_info['parent_field_name']};
}
if ($missing_tid_parentid != '') {
$parent_data = MYMODULE_fetch_category_info($missing_tid_parentid, $relation_ship_info);
MYMODULE_complete_term_relationship($missing_content_data, $parent_data, $relation_ship_info);
}
}
/**
* Fetch parent terms for missing terms
* @param [type] $missing_tid_parentid
* @param [type] $relation_ship_info
* @return [type] [description]
*/
function MYMODULE_fetch_category_info($missing_tid_parentid, $relation_ship_info) {
$parent_data = array();
$query = "SELECT sourceid1, destid1 FROM " . $relation_ship_info['map_table'] . "
WHERE sourceid1 in (" . $missing_tid_parentid. ")";
$result = db_query($query);
foreach($result as $data) {
$parent_data[$data->sourceid1] = $data;
}
return $parent_data;
}
/**
* Update taxonomy_term_hierarchy for missing parents
* @param [type] $missing_content_data [description]
* @param [type] $parent_data [description]
* @param [type] $parent_data [description]
* @param [type] $relation_ship_info [description]
* @return [type] [description]
*/
function MYMODULE_complete_term_relationship($missing_content_data, $parent_data, $relation_ship_info) {
$total = 0;
$output = "";
foreach ($missing_content_data as $key => $data) {
$missing_tid = $data->destid1;
$ptid = $data->{$relation_ship_info['parent_field_name']};
$missing_parent_tid = $parent_data[$ptid]->destid1;
if (!$missing_parent_tid) {
continue;
}
$total++;
db_update("taxonomy_term_hierarchy") ->condition('tid', $missing_tid)
->fields(array('parent' => $missing_parent_tid))
->execute();
$output .= "
Map $missing_tid -> $missing_parent_tid";
}
echo "
Repaired total ::", $total, " terms";
print $output;
}
Also read the developer tip on
Hide Taxonomy Terms from Facets Display through Interface.
Our Services
Customer Experience Management
- Content Management
- Marketing Automation
- Mobile Application Development
- Drupal Support and Maintanence
Enterprise Modernization, Platforms & Cloud
- Modernization Strategy
- API Management & Developer Portals
- Hybrid Cloud & Cloud Native Platforms
- Site Reliability Engineering