Answers.Body: <p>One possibility could be importing the data as it is (maybe using a temporary field for that column) and use 
  <a href="http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_post" rel="nofollow">hook_civicrm_post()</a> to manipulate the contribution just after creation:</p>
  
  <pre><code>function example_civicrm_post($op, $objectName, $objectId, &amp;$objectRef) {
  
    if ($objectName == 'Contribution' &amp;&amp; $op == 'create') {
  
      if (!empty($objectRef-&gt;temporary_field)) {
  
        // Preprocess field. FIXME: do some validation first.
        $objectRef-&gt;real_field = do_awesome_stuff($objectRef-&gt;temporary_field);
  
        // Clear temporary field.
        $objectRef-&gt;temporary_field = '';
      }
    }
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-24T20:25:16.633Z
Answers.LastActivityDate: 2015-03-24T20:25:16.633Z
Answers.UserDisplayName: Pere
Body: <p>We use <a href="http://book.civicrm.org/user/current/common-workflows/importing-data/" rel="nofollow">CiviCRM's import data functionality</a> (and <a href="https://civicrm.org/extensions/api-csv-import-gui" rel="nofollow">api-csv-import-gui</a>) to import partner's contributions from CSV files. This is working great because the CSV columns can be mapped to CiviCRM fields via a graphical user interface.</p>
  
  <p>However, we've recently had the need to preprocess one of these fields and transform their data on import. This process can't be done outside CiviCRM for security reasons.</p>
  
  <p>Can I still use CiviCRM's point-and-click mapping functionality while being able to preprocess a specific CSV column in PHP? Unfortunately I can't just map the columns-fields in code because the CSV columns change.</p>
  
Tags: <civimember><import><civicontribute><hooks>
Title: How do we preprocess CSV columns when using CiviCRM builtin import functionality?
ThreadText: # How do we preprocess CSV columns when using CiviCRM builtin import functionality?
  
  (Asked by: username_0 on 2015-03-24)
  
  We use [CiviCRM's import data functionality](http://book.civicrm.org/user/current/common-workflows/importing-data/) (and [api-csv-import-gui](https://civicrm.org/extensions/api-csv-import-gui)) to import partner's contributions from CSV files. This is working great because the CSV columns can be mapped to CiviCRM fields via a graphical user interface.
  
  However, we've recently had the need to preprocess one of these fields and transform their data on import. This process can't be done outside CiviCRM for security reasons.
  
  Can I still use CiviCRM's point-and-click mapping functionality while being able to preprocess a specific CSV column in PHP? Unfortunately I can't just map the columns-fields in code because the CSV columns change.
  
  **Tags:** civimember, import, civicontribute, hooks
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2015-03-24. Score: 3
  
  One possibility could be importing the data as it is (maybe using a temporary field for that column) and use
  [hook\_civicrm\_post()](http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_post) to manipulate the contribution just after creation:
  
  ```
  function example_civicrm_post($op, $objectName, $objectId, &$objectRef) {
  
    if ($objectName == 'Contribution' && $op == 'create') {
  
      if (!empty($objectRef->temporary_field)) {
  
        // Preprocess field. FIXME: do some validation first.
        $objectRef->real_field = do_awesome_stuff($objectRef->temporary_field);
  
        // Clear temporary field.
        $objectRef->temporary_field = '';
      }
    }
  }
  
  ```
  
  
  ---
  
  