function content_taxonomy_node_import_values_alter in Node import 6
Implementation of hook_node_import_values_alter()
File
- supported/
content_taxonomy/ content_taxonomy.inc, line 38 - Support file for the CCK Content Taxonomy module
Code
function content_taxonomy_node_import_values_alter(&$values, $type, $defaults, $options, $fields, $preview) {
// Place the following line in the validation function on the content_taxonomy module in order to determine
// what the widget is expecting:
//
// drupal_set_message( '<pre>Element: ' . print_r( $element, TRUE ) . '</pre>' );
//
// For example, I placed the above line in the content_taxonomy_tree_validate function in order to discover
// the data structure that the validator expects. I assume the other widgets expect different structures.
foreach (node_import_cck_fields($type, 'content_taxonomy') as $fieldname => $fieldinfo) {
$vid = $fieldinfo['vid'];
//***************************************tree widget***************************************
if ($fieldinfo['widget']['type'] == 'content_taxonomy_tree') {
$terms = array();
$results = array();
foreach ($values[$fieldname] as $i => $value) {
if (is_numeric($value['value'])) {
$foundterms = taxonomy_get_term(intval($value['value']));
}
else {
$value['value'] = content_taxonomy_node_import_check_numeric($value['value']);
$foundterms = taxonomy_get_term_by_name($value['value']);
}
foreach ($foundterms as $index => $termfound) {
if ($termfound->vid == $vid) {
$tid = $termfound->tid;
$terms[$tid] = (array) $termfound;
$children = taxonomy_get_children($tid, $vid);
$terms[$tid]['children'] = $children;
$parents = taxonomy_get_parents($tid);
$terms[$tid]['parents'] = $parents;
}
}
}
foreach ($terms as $term_id => $data) {
if (empty($data['parents'])) {
// We only want top-level terms here
$results['value'][$term_id] = array(
'checkbox' => $term_id,
);
if (!empty($data['children'])) {
// Handle the children terms
content_taxonomy_node_import_tree_adjust($terms, $data, $results['value'][$term_id]['children']);
}
else {
unset($terms[$term_id]);
}
}
}
$values[$fieldname] = $results;
}
elseif ($fieldinfo['widget']['type'] == 'content_taxonomy_options') {
// Content taxonomy checkboxes
$results = array();
foreach ($values[$fieldname] as $i => $value) {
if (is_numeric($value['value'])) {
// Using TID...
$sql = "SELECT tid FROM {term_data} WHERE vid = %d AND tid = %d";
if ($tid = db_result(db_query($sql, $vid, intval($value['value'])))) {
$results[$tid] = $tid;
}
}
else {
// Using term name...
$value['value'] = content_taxonomy_node_import_check_numeric($value['value']);
$sql = "SELECT tid FROM {term_data} WHERE vid = %d AND LOWER(name) = '%s'";
if ($tid = db_result(db_query($sql, $vid, drupal_strtolower($value['value'])))) {
$results[$tid] = $tid;
}
}
}
$values[$fieldname]['value'] = $results;
}
elseif ($fieldinfo['widget']['type'] == 'content_taxonomy_hs') {
$terms = array();
$results = array();
foreach ($values[$fieldname] as $i => $value) {
$position = stripos($value['value'], '>>');
if ($position) {
//using the delimiter, we are given the explicit parentage of the term. we can therefore have duplicate terms.
$lineage = explode('>>', $value['value']);
$terms = content_taxonomy_node_import_get_children($lineage, $vid);
}
else {
//we have just one term, hopefully there are no duplicates, so let's find the parents
if (is_numeric($value['value'])) {
$foundterms = taxonomy_get_term(intval($value['value']));
}
else {
$value['value'] = content_taxonomy_node_import_check_numeric($value['value']);
$foundterms = taxonomy_get_term_by_name($value['value']);
}
foreach ($foundterms as $index => $termfound) {
if ($termfound->vid == $vid) {
$tid = $termfound->tid;
$terms[$tid] = (array) $termfound;
$children = taxonomy_get_children($tid, $vid);
$terms[$tid]['children'] = $children;
$parents = taxonomy_get_parents($tid);
$terms[$tid]['parents'] = $parents;
}
}
}
}
//We now have our list of hierarchical terms in order of parent>>child>>child, etc. Now let's arrange them how
//hierarchical select expects them to be.
if (!empty($terms)) {
foreach ($terms as $index => $data) {
$results[] = $data['tid'];
}
$values[$fieldname]['tids'] = $results;
}
else {
$values[$fieldname]['tids'] = NULL;
}
}
else {
foreach ($values[$fieldname] as $i => $value) {
if (is_numeric($value['value'])) {
// Using TID...
$sql = "SELECT tid FROM {term_data} WHERE vid = %d AND tid = %d";
if ($tid = db_result(db_query($sql, $vid, intval($value['value'])))) {
$values[$fieldname][$i]['value'] = $tid;
}
else {
$value['value'] = content_taxonomy_node_import_check_numeric($value['value']);
if (trim($value['value']) != '') {
$edit = array(
'vid' => $vid,
'name' => $value['value'],
);
taxonomy_save_term($edit);
$sql = "SELECT tid FROM {term_data} WHERE vid = %d AND tid = %d";
if ($tid = db_result(db_query($sql, $vid, intval($value['value'])))) {
$values[$fieldname][$i]['value'] = $tid;
}
}
}
}
else {
// Using term name...
$sql = "SELECT tid FROM {term_data} WHERE vid = %d AND LOWER(name) = '%s'";
if ($tid = db_result(db_query($sql, $vid, drupal_strtolower($value['value'])))) {
$values[$fieldname][$i]['value'] = $tid;
}
else {
$value['value'] = content_taxonomy_node_import_check_numeric($value['value']);
if (trim($value['value']) != '') {
$edit = array(
'vid' => $vid,
'name' => $value['value'],
);
taxonomy_save_term($edit);
$sql = "SELECT tid FROM {term_data} WHERE vid = %d AND LOWER(name) = '%s'";
if ($tid = db_result(db_query($sql, $vid, drupal_strtolower($value['value'])))) {
$values[$fieldname][$i]['value'] = $tid;
}
}
}
}
}
}
}
}