This is a bit is an issue user_import does not save taxonomy field references I get the following error

Notice: Undefined index: tid in taxonomy_field_presave() (line 1889 of /modules/taxonomy/taxonomy.module).

Whilst I can see 1889 I cant find where in the user_upload module the pre-save is invoked.

It's rather urgent for me and any pointer in getting this resolved would be appreciated.

Thanks

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

eminencehealthcare’s picture

Same problem here. It would be really useful to have the user import module be able to fill term reference fields (not just text fields)

caesius’s picture

I've been able to figure out essentially what the problem is.

On line 48 of the file supported/field.inc:

$user_fields = user_load($account->uid);

For the taxonomy fields, instead of an empty array the user_load function gives back this data:

    [und] => Array
        (
            [0] => Array
                (
                    [tid] =>
                )

        )

It then adds the data entered for the user info as the "value:"

    [und] => Array
        (
            [0] => Array
                (
                    [tid] =>       #Note: This shows up if you previously tried to save data for this user and the taxonomy field failed to save; the notice below is the same regardless
                    [value] => Bangladesh
                )

        )

When it tries to save the taxonomy field in the function taxonomy_field_presave, the term ID is undefined, which will generate this notice:

Notice: Undefined index: tid in taxonomy_field_presave() (line 1889 of /var/git/working-copies/abielefe/d7_sandbox/htdocs/modules/taxonomy/taxonomy.module).

Running on core version 7.21 and module version 1.0-beta5.

caesius’s picture

This patch is barely tested, but it did work for the very simple test I performed with it. Will need to to more rigorous testing, including seeing if the term autocreation works correctly.

caesius’s picture

Disregard the previous patch; it generates errors when either a term doesn't exist or nothing has been entered for the field.

Updated patch fixes these issues. Ideally, however, there should be a way to enter multiple taxonomy terms into a single field. One way would be to enclose the field in quotes, require that the terms be separated by commas e.g. "Orange,Banana,Peach", and treat that as its own CSV. However, commas are allowed in taxonomy term names (as is seemingly all other punctuation), so this would not be a foolproof solution.

caesius’s picture

Got it. The solution is to format terms which have the delimiter character as "first term,\"second, term\""

New patch now has complete taxonomy importing capabilities. Some notes:

1. As mentioned above, when importing multiple taxonomy terms the column must be enclosed with double quotes and each term must be separated with the delimiter character within the quotes, e.g. "Orange,Banana,Peach"

2. When the delimiter character is used inside a term, escaped double quotes must surround the term within the outer quotes, e.g. "\"This term, has a comma\""

3. The delimiter character selected in the settings is what must be used for the taxonomy term field as well. For example, if the pipe | character is used, a CSV might look like this:
username|email|"term one|\"term | two\""

4. This patch also fixes an apparently unreported bug whereby tab delimiters do not work.

eminencehealthcare’s picture

This patch does not work for me when using a term reference field of any kind.

I see it has been committed to the latest dev version and that also does not seem to work.

I am getting two errors:

This one appears in my cron log under user import module:

  • Warning: explode(): Empty delimiter in field_user_import_after_save() (line 58 of /var/www/vhosts/eminencehc.com/httpdocs/sites/all/modules/user_import/supported/field.inc).

And this one shows on the user account page:

  • Warning: array_flip(): Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->load() (line 178 of /var/www/vhosts/eminencehc.com/httpdocs/includes/entity.inc).
caesius’s picture

Status: Active » Needs review
FileSize
3.99 KB

Actually the only thing in my patch that has been applied is the additional tab options, nothing else. The patch no longer applies to the latest dev version anyway, and I accidentally left in a call to flog_it for file logging (which was probably what was giving you problems). Here is an updated dev patch (which I admittedly haven't tested).

chaloum’s picture

Just tried this It still not importing information that referes to term references, a bit sad

eminencehealthcare’s picture

Me too Chaloaum. I recommend using the feeds module. It wasn't as easy to set up and as convenient as this module is but it did the job.

cprouvot’s picture

I create a path for use taxonomy field if you put the tid in your CSV file.

cprouvot’s picture

The patch above

Tritof’s picture

I tried the patch #11 above, it doesn"t work, error message:

Notice: Undefined index: tid in taxonomy_field_presave() (line 1894 of .../website/modules/taxonomy/taxonomy.module).

So it is about this line:

if ($item['tid'] == 'autocreate') {

down there:

/**
 * Implements hook_field_presave().
 *
 * Create any new terms defined in a freetagging vocabulary.
 */
function taxonomy_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
  foreach ($items as $delta => $item) {
   if ($item['tid'] == 'autocreate') {
      $term = (object) $item;
      unset($term->tid);
      taxonomy_term_save($term);
      $items[$delta]['tid'] = $term->tid;
    }
  }
}
szt’s picture

Version: 7.x-1.0-beta4 » 7.x-2.x-dev
FileSize
3.99 KB

Attached the patch for the latest 7.x-2.x dev.
Works well :)
(PHP 5 >= 5.3.0)

szt’s picture

Status: Needs review » Needs work

Doesn't work with multiple terms...

This line has a small typo btw:

$values = str_getcsv(stripcslashes($values[0]), $settings['delimiter']);

stripslashes not stripcslashes

The real problem is that $values must be an array of terms, but after this line it has only one term (with ";" delimiter and with "||" multiple value delimiter).

szt’s picture

With this simpler patch the multiple terms works well.

The csv file contains this:

username1;email1;customfield1;term1a||term1b||term1c
username2;email2;customfield2;term2a||term2b
username3;email3;customfield3;term3a||term3b||term3c||term4d

So I set the ";" and "||" delimiters properly at admin/people/user_import/add/[IMPORT_ID].

szt’s picture

Status: Needs work » Needs review
radziaziz’s picture

I can confirm patch #15 works with 7.x-2.2. Thanks alot, you saved my day!

szt’s picture

Status: Needs review » Reviewed & tested by the community

  • Robert Castelo committed c89142b on 7.x-2.x
    Issue #1887400 by caesius, szt, cprouvot: does not save taxonomy fields
    
Robert Castelo’s picture

Status: Reviewed & tested by the community » Fixed

Patch #15 committed to 7.x-2.x-dev branch.

Will roll a new release once I've gone through the other ready for review issues.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.