I'm having a problem working with any values uploaded with node_import. Some fields need to get changed or rewritten, some need to get unset, etcetera. Using hooks from the module or Drupal, I'm only seeing values from the CSV file that passed validation and not the one's I'd like to fix.

What am I missing here? Specifically, I'd like to rework some date fields and get Excel mapped to a short date. ('have seen the traffic on this) How can I find the values from the upload, before they're being validated?

These tries didn't show me the upload values:

function gswwimport_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
	if($op == 'validate' && $node->type == 'event')
	{ dsm(func_get_args()); }
}
function gswwimport_form_alter(&$form, &$form_state, $form_id) 
{
	if($form_id == 'event_node_form') 
	{ dsm(func_get_args()); }
}
function gswwimport_node_import_values_alter(&$values, $type, $defaults, $options, $fields, $preview) {
	dsm(func_get_args()); 
}
function gswwimport_node_import_fields_alter(&$fields, $type) 
{
	dsm(func_get_args()); 
}

Comments

Robrecht Jacques’s picture

Title: How do I alter imported values » How do I alter imported values?
Version: 6.x-1.x-dev » 6.x-1.1
Status: Active » Needs review

If you want to change one specific field for one specific content type, you can use:

/**
 * Implements hook_node_import_fields_alter().
 */
function gswwimport_node_import_fields_alter(&$fields, $type) {
  // If the import is about the 'event' node type
  if (($node_type = node_import_type_is_node($type)) == 'event') {
    // Add a preprocessing function, in front of all present functions, eg
    // before node_import_check_date() etc.
    array_unshift($fields['somefield']['preprocess'], 'gswwimport_my_handling_function');

    // Alternatively, you could change the whole 'preprocess' array
    // to only include the callbacks you want.
  }
}

/**
 * Callback, similar to node_import_check_*() functions
 * in node_import.inc.
 */
function gswwimport_my_handling_function(&$value, $field, $options, $preview) {
  // Here you would change $value, possibly depending on $options
  // (which would have to be set with hook_node_import_options()).
  $value = some_clever_date_parsing_function($value);

  // You could also do validation here (before or after):
  if ($value > 1234) {
    node_import_input_error("Value too big!");
    return FALSE; // validation stops further preprocessing.
  }

  // return TRUE; // if this is the final preprocessing function to run,
  // return FALSE; // if there is some validation problem,
  return; // if the other preprocessing functions are called as well.

  // So, $value will contain the transformed value, do not return it!
}

Alternatively, you can check the 'input_format' key of the field definition to add your handler to all fields of 'date' type:

/**
 * Implements hook_node_import_fields_alter().
 */
function gswwimport_node_import_fields_alter(&$fields, $type) {
  foreach ($fields as $fieldname => $fieldinfo) {
    if ($fieldinfo['input_format'] == 'date') {
      array_unshift($fields[$fieldname]['preprocess'], 'gswwimport_my_handling_function');
    }
  }
}

Somewhat untested, but that is the idea of it.

Robrecht Jacques’s picture

Title: How do I alter imported values? » API info - How do I alter imported values?
Priority: Major » Normal
Status: Needs review » Active
Issue tags: +example use, +document hooks
avpaderno’s picture

Issue summary: View changes
Status: Active » Closed (outdated)
Issue tags: -example use, -document hooks

I am closing this issue, since it's for a Drupal version no longer supported.