hello and thank you for your work.

I would like to add a couple of small features: hooks to modify the header and the rows of the submitted CSV file before they are parsed by uif.

The rational is to "fix" the CSV file, whatever it means. In my use case the CSV file is exported from outlook by a non-technical person so I need to do some customizations before importing users, e.g.

- fill incomplete rows
- set custom key for email address ("Indirizzo E-mail")

I'll try to provide a patch in the next couple of days, please let me know if you're interested in accepting it.

Regards,
Maurizio

Comments

mauriziopinotti’s picture

And here's the patch:

--- uif.admin.inc.orig	2011-10-10 23:37:55.000000000 +0200
+++ uif.admin.inc	2011-10-12 10:39:17.000000000 +0200
@@ -170,7 +170,9 @@
   
   if ($fp = fopen($uri, 'r')) {
     // Read the header
-    $header_row = uif_normalize_header(array_map('trim', fgetcsv($fp)));
+    $header_row = fgetcsv($fp);
+    drupal_alter('uif_header', $header_row);
+    $header_row = uif_normalize_header(array_map('trim', $header_row));
     $line++;
     $errors = module_invoke_all('uif_validate_header', $header_row);
     uif_add_line_number($errors, $line);
@@ -183,6 +185,7 @@
     $errors = array();
     while (!feof($fp) && (count($errors) < 20)) {
       $row = fgetcsv($fp);
+      drupal_alter('uif_row', $row, $header_row);
       $line++;
       if (uif_row_has_data($row)) {
         $user_row = array_combine($header_row, array_map('trim', $row));

Now you can alter header and rows with hooks like

/**
 * Implements hook_uif_header_alter().
 */
function strutturainformatica_uif_header_alter(&$header_row) {
  foreach ($header_row as $key => $value) {
    // Fix "email" field
    if (strtolower(trim($value)) == 'indirizzo posta elettronica') $header_row[$key] = 'email';
  }
}

/**
 * Implements hook_uif_row_alter().
 */
function strutturainformatica_uif_row_alter(&$row, $header_row) {
  // ...
}
dwightaspinwall’s picture

Status: Active » Closed (fixed)

Added to 6.x-1.5 / 7.x-1.0-beta8 Thanks!