I want to import matrix field by import CSV(node_import). But now it doesn't supported.So i create a matrix.inc file in node_import/supported/cck. It's works well,but when first i import a supplier node, then i import a product reference this supplier,but after i import the product ,the supplier's matrix fields's data is missing.So i want know how node_import store a reference, restore the node's all fields's data that be referenced?
node_import/supported/cck/matrix.inc


function matrix_node_import_fields($type) {
    $fields = array();
    foreach (node_import_cck_fields($type, 'matrix') as $fieldname => $fieldinfo) {

        $cck_fieldname = node_import_cck_name($fieldname, $colname);
        $fields[$cck_fieldname] = array(
            'title' => $fieldinfo['widget']['label'],
            'tips' => array(
                t('Imports into %name CCK field (%type type).', array('%name' => $fieldinfo['field_name'], '%type' => $fieldinfo['type'])),
            ),
            'has_multiple' => 1,
            'module' => $fieldinfo['module'],
            'allowed_values' => content_allowed_values($fieldinfo),
            'cck:fieldname' => $fieldinfo['field_name'],
            'cck:fieldinfo' => $fieldinfo,
        );
    }
    return $fields;
}

function matrix_node_import_values_alter(&$values, $type, $defaults, $options, $fields, $preview) {
    foreach (node_import_cck_fields($type, 'matrix') as $fieldname => $fieldinfo) {
        $cck_fieldname = node_import_cck_name($fieldname, $colname);

        if ($fieldinfo['widget']['type'] == 'table') {
            $cck_value = $values[$cck_fieldname];
            if (!empty($cck_value)) {
                foreach ($cck_value as $row => $column) {
                    if (strpos($column, '>>')) {
                        $columns = explode('>>', $column);
                        $values[$cck_fieldname][$row] = $columns;
                    }
                }
            }
        }
    }
}

function matrix_node_import_postprocess($type, $values, $options, $preview) {
    if (!$preview && isset($values['nid'])) {
        //Inset matrix value into table {node_field_matrix_data} use own php code.
        $node_nid = $values['nid']; //nid
        $vid = db_result(db_query('SELECT vid FROM {node} WHERE nid = %d', $node_nid)); //vid
        foreach (node_import_cck_fields($type, 'matrix') as $fieldname => $fieldinfo) {
            $query = '';

            $cck_fieldname = node_import_cck_name($fieldname, $colname);
            $prepare_data = $values[$cck_fieldname]; //value pass from csv file.

            $col_info = count(unserialize($fieldinfo['cols_elements'])); //to get column number
            $value_query = '';
            $j = 0;
            // build insert query value according to the row/column settings
            foreach ($prepare_data as $row => $d) {
                for ($i = 0; $i < $col_info; $i++, $j++) {
                    if ($j !== 0) {
                        //only the (0,0) cells do not need a comma
                        $value_query .= ' ,';
                    }
                    $value_query .= ' ("' . $node_nid . '",  "' . $vid . '",  "' . $fieldname . '",  "' . $row . '",  "' . $i . '",  "' . $prepare_data[$row][$i] . '") ';
                }
            }
            //do nothing if here is empty.
            if (!empty($value_query)) {
                $query = 'INSERT INTO  {node_field_matrix_data} (`nid` ,`vid` ,`field_name` ,`row` ,`col` ,`value`) '
                        . 'VALUES ' . $value_query;
                db_result(db_query($query));
            }
        }
    }
}

Comments

jerry.xu’s picture

Title: Can matrix fileds be imported by import CSV files? » Can matrix fields be imported by import CSV files?
intrafusion’s picture

Status: Active » Closed (won't fix)