function node_import_values in Node import 6
Create an array of values to submit to the form.
Parameters
$type: String. The node_import type.
$data: Array of data from the file as ($col_index => $value).
$map: Array of how the data maps to fields.
$defaults: Array of default values.
$options: Array of options.
$fields: Array of available fields.
$preview: Boolean. If TRUE a preview will be created. If FALSE construct the values for real.
Return value
Array of values to submit to a form. See hook_node_import_values().
Related topics
1 call to node_import_values()
- node_import_create in ./
node_import.inc - Create a new object of specified $type.
File
- ./
node_import.inc, line 575 - Public API of the Node import module.
Code
function node_import_values($type, $data, $map, $defaults, $options, $fields, $preview) {
$values = module_invoke_all('node_import_values', $type, $defaults, $options, $fields, $preview);
foreach ($fields as $fieldname => $fieldinfo) {
$map[$fieldname] = isset($map[$fieldname]) ? $map[$fieldname] : '';
$map[$fieldname] = is_array($map[$fieldname]) ? $map[$fieldname] : array(
$map[$fieldname],
);
$map_count = node_import_field_map_count($fieldname, $map);
$mseparator = isset($options[$fieldname]['multiple_separator']) ? $options[$fieldname]['multiple_separator'] : $fieldinfo['multiple_separator'];
$hseparator = isset($options[$fieldname]['hierarchy_separator']) ? $options[$fieldname]['hierarchy_separator'] : $fieldinfo['hierarchy_separator'];
$hreverse = isset($options[$fieldname]['hierarchy_reverse']) ? $options[$fieldname]['hierarchy_reverse'] : $fieldinfo['hierarchy_reverse'];
// Merge default value for each field.
if (isset($defaults[$fieldname])) {
if ($fieldinfo['is_checkboxes']) {
$values[$fieldname] = array_keys(array_filter($defaults[$fieldname]));
}
else {
$values[$fieldname] = $defaults[$fieldname];
}
}
else {
if (isset($fieldinfo['default_value'])) {
$values[$fieldname] = $fieldinfo['default_value'];
}
}
// Map the data ONLY IF the data to map is not empty.
if ($fieldinfo['has_multiple']) {
if ($map_count > 0) {
$fieldvalues = array();
foreach ($map[$fieldname] as $col) {
$value = isset($data[$col]) ? (string) $data[$col] : '';
if ($map_count == 1 && strlen($mseparator) > 0) {
$fieldvalues = strlen($value) > 0 ? array_map('trim', explode($mseparator, $value)) : array();
break;
}
$fieldvalues[] = $value;
}
if (!$fieldinfo['allow_empty']) {
$fieldvalues = array_filter($fieldvalues, 'drupal_strlen');
}
if ($fieldinfo['has_hierarchy'] && strlen($hseparator) > 0) {
foreach ($fieldvalues as $i => $value) {
$fieldvalues[$i] = strlen($value) > 0 ? array_map('trim', explode($hseparator, $value)) : array(
$value,
);
}
}
if (empty($fieldvalues) && isset($values[$fieldname])) {
$values[$fieldname] = $values[$fieldname];
}
else {
$values[$fieldname] = $fieldvalues;
}
}
}
else {
if ($map_count > 0 && $fieldinfo['has_hierarchy']) {
$fieldvalues = array();
foreach ($map[$fieldname] as $col) {
$value = isset($data[$col]) ? (string) $data[$col] : '';
if ($map_count == 1 && strlen($hseparator) > 0) {
$fieldvalues = drupal_strlen($value) > 0 ? array_map('trim', explode($hseparator, $value)) : array();
break;
}
$fieldvalues[] = $value;
}
if (!$fieldinfo['allow_empty']) {
$fieldvalues = array_filter($fieldvalues, 'drupal_strlen');
}
if ($hreverse) {
$fieldvalues = array_reverse($fieldvalues);
}
$values[$fieldname] = empty($fieldvalues) ? $values[$fieldname] : $fieldvalues;
}
else {
if ($map_count == 1) {
foreach ($map[$fieldname] as $col) {
$value = isset($data[$col]) ? (string) $data[$col] : '';
}
$values[$fieldname] = drupal_strlen($value) > 0 ? $value : (isset($values[$fieldname]) ? $values[$fieldname] : '');
}
}
$values[$fieldname] = array(
isset($values[$fieldname]) ? $values[$fieldname] : '',
);
}
// Preprocess the data as long as the value is not empty and it
// validates.
$values[$fieldname] = isset($values[$fieldname]) ? $values[$fieldname] : array(
'',
);
foreach ((array) $values[$fieldname] as $i => $value) {
foreach ($fieldinfo['preprocess'] as $function) {
if (is_array($values[$fieldname][$i]) && !empty($values[$fieldname][$i]) || is_string($values[$fieldname][$i]) && drupal_strlen($values[$fieldname][$i]) > 0) {
$options[$fieldname] = isset($options[$fieldname]) ? $options[$fieldname] : array();
$value = $values[$fieldname][$i];
$return = $function($value, $fieldinfo, $options[$fieldname], $preview);
$values[$fieldname][$i] = $value;
if ($return === FALSE) {
// No longer validates - stop.
$values[$fieldname][$i] = '';
continue 2;
}
else {
if ($return === TRUE) {
// Final preprocess reported - stop.
continue 2;
}
}
}
}
}
// If empty values are not allowed, filter them out.
if (!$fieldinfo['allow_empty']) {
$values[$fieldname] = array_filter((array) $values[$fieldname], 'drupal_strlen');
}
// Handle files specially. The preprocess function only returns
// the path - we need to make sure we save the file now into the
// db and set the value to the fid. We need to do this here instead
// of in the preprocess function because we need $values['uid'].
if ($fieldinfo['input_format'] == 'filepath') {
foreach ($values[$fieldname] as $i => $value) {
if (drupal_strlen($value) > 0) {
$result = db_result(db_query("SELECT fid FROM {files} WHERE filepath = '%s'", $value));
if ($result) {
$values[$fieldname][$i] = $result;
}
else {
// TODO: don't we need more stuff - eg run the validators?
global $user;
$file = new stdClass();
$file->uid = isset($values['uid']) ? $values['uid'] : $user->uid;
$file->filename = basename($value);
$file->filepath = $value;
$file->filesize = filesize($value);
$file->filemime = file_get_mimetype($file->filename);
$file->status = FILE_STATUS_TEMPORARY;
$file->timestamp = time();
drupal_write_record('files', $file);
$values[$fieldname][$i] = $file->fid;
}
}
}
}
// If only a single value is allowed, get the first one.
if (!$fieldinfo['has_multiple']) {
$values[$fieldname] = array_shift($values[$fieldname]);
}
// Convert checkboxes fields to a format FAPI understands.
if ($fieldinfo['is_checkboxes'] && !empty($values[$fieldname])) {
// Only in PHP > 5.2: $values[$fieldname] = array_fill_keys($values[$fieldname], 1);
$values[$fieldname] = array_combine($values[$fieldname], array_fill(0, count($values[$fieldname]), 1));
}
}
drupal_alter('node_import_values', $values, $type, $defaults, $options, $fields, $preview);
return $values;
}