One of my Vocabularies contains numeric string-vaules, e.g. '2010', '2020'.
If I import a csv-file containing nodes with numeric terms, the terms get translated to their term name, but to their term ID.
See the test results in below diagram:

Vocabulary Feeds importer Result
tid | Name Name | Term Name | Term
10 | 2010 first |2010 first |
11 | 2020 second|11 second| 2020
12 | X010 third |X010 third | X010

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Andrey Zakharov’s picture

as temporary solution comment out code in mappers/taxonomy.inc: 92:

    elseif (is_numeric($term)) {
      $tid = $term;
    }
johnv’s picture

Title: Field mapping fails for Numeric taxonomy term » Split taxonomy mapper in two variants: term name +tid (avoids mapping error for Numeric taxonomy term, too)
Status: Active » Needs review

The following 'patch' splits the taxonomy mapper in two: 'Term name' and 'Term ID'.
This avoids the mapping error with numeric term values, and gives better control by the user.

 function taxonomy_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
   foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) {
     $info = field_info_field($name);
     if ($info['type'] == 'taxonomy_term_reference') {
 
       $targets[$name] = array(
-        'name' => $instance['label'],
-        'callback' => 'taxonomy_feeds_set_target',
+        'name' => $instance['label'] . ': Term name',
+        'callback' => 'taxonomy_feeds_set_target_name',
         'description' => t('The @label field of the node.', array('@label' => $instance['label'])),
+//        'real_target' => $name,
       );
+      $targets[$name . ':tid'] = array(
+        'name' => $instance['label'] . ': Term ID',
+        'callback' => 'taxonomy_feeds_set_target',
+        'description' => t('The @label field of the node.', array('@label' => $instance['label'])),
+//        'real_target' => $name,
+      );
    }
  }
}
 
+function taxonomy_feeds_set_target_tid($source, $entity, $target, $terms) {
+  $variant = 'tid';
+  taxonomy_feeds_set_target($source, $entity, $target, $terms, $variant);
+}
+
+function taxonomy_feeds_set_target_name($source, $entity, $target, $terms) {
+  $variant = 'name';
+  taxonomy_feeds_set_target($source, $entity, $target, $terms, $variant);
+}

/**
 * Callback for mapping. Here is where the actual mapping happens.
 *
 * @todo Do not create new terms for non-autotag fields.
 */
-function taxonomy_feeds_set_target($source, $entity, $target, $terms, $variant) {
+function taxonomy_feeds_set_target($source, $entity, $target, $terms, $variant='name') {

and in the function itself:

    $tid = 0;
    if ($term instanceof FeedsTermElement) {
      $tid = $term->tid;
    }
-   elseif (is_numeric($term)) {
+    elseif ((is_numeric($term)) && ($variant == 'tid')) { 
      $tid = $term;
    }
    elseif (is_string($term)) {
      $tid = taxonomy_term_check_term($term, $vocabulary->vid);
    }
7wonders’s picture

I confirm the above works. My question would be is it possible to dynamically do this with all the fields of a given taxonomy so that all fields for the term are exposed to be mapped to (image etc) ?

XiaN Vizjereij’s picture

Subscribe

johnv’s picture

@7wonders,
do you want to map to ANY field in the content type? then check this issue: #1111412: How to use unique target to update imported nodes?

7wonders’s picture

Thanks for the suggestion johnv but no, not really what im after. I know that its possible to import to taxonomy and then import to nodes referencing those terms (with d7 this means that you can then have a wealth of fields in the term etc). But what I was after is skipping the importing to the taxonomy part first and doing straight off the node import. At present the taxonomy field of the node is exposed to be imported into (and this patch allows for term name or term id) but not the other associated fields.

johnv’s picture

Ah, I understand. I think you need 2 importers. But you can make a super import file, and run it twice: once for the terms, and once for the nodes.

eiriksm’s picture

subscribing

Anonymous’s picture

Great module :) I'm using alpha4

I have taxonomy terms as years, 2000,1999,1998 etc and in my database, the term '2000' has a tid of 30.

If I do a csv node import with '2000' linked to my term reference field I don't get any result. However, if I do a node import with '30' I get my '2000' taxonomy term correctly referenced in the newly created node.

#1 proved a quick fix for the data I was working on :)

Summit’s picture

Subscribing,
I was wondering what went wrong, trying for hours then found this issue.
#1 worked for me!
Would be great if solution would be committed please.
greetings, Martijn

Steven.Pescador’s picture

Hi all,

Stumbled upon this thread when faced with a similar problem today. Some of the term names in one of my vocabularies are numeric ie. 004 - which during import, are being assumed related to term id rather than term name care of this condition ---- elseif (is_numeric($term)) { $tid = $term; }

I'm aware I could use the term id in my csv, but what if my client is using this csv for updates and only knows the term names..? (would rather not confuse them as well) Do I have any other options apart from kitten killing..?

Apologies if you think this tangent isn't related, seemed to be a scenario that I thought more than just myself would face..?

Thanks for your time.

**Edit**

Seems my trail of rookiness left on the threads of Drupal has now grown larger...

Applied patch #2 and my import of numeric taxonomy terms worked flawlessly. (with no need of knowing the term id obviously).

Note to self - read each comment more thoroughly. Thanks johnv, much appreciated.

ts145nera’s picture

subscribe, #2 work for me
Thank You

ts145nera’s picture

I export the #2 in patch.
Please review

tyler-durden’s picture

I'm assuming this will not work for 6.x versions? If not, would it be too much work to port over?

johnv’s picture

tyler-durden, it shouldn't be to hard. Just open your php-file and try to copy-and-paste as much as possible.

tyler-durden’s picture

Thanks for the tip, I looked at it and it seems easy but after I figured out my original problem so I don't need to use the ID # anymore.

TechNikh’s picture

had same issue. Glad I saw this post

TechNikh’s picture

I comment out code in mappers/taxonomy.inc: 92:

<?php
    elseif (is_numeric($term)) {
      $tid = $term;
    }
?>

but if the number is zero (0) it didn't get imported

7wonders’s picture

This patch is working great and together with the term_hierarchy extension to feeds tamper I can now import hierarchy straight into nodes. Nice!

7wonders’s picture

Scrap my last comment, there are still some issues with importing hierarchy if having to use this patch.

itamair’s picture

FileSize
1.99 KB

there was a littel error in the #2 code (second callback function should be:'callback' => 'taxonomy_feeds_set_target_tid') and the patch #14 didn't apply for me.
So I found my fixes.
The final taxonomy.inc that works for me is here attached. I guess should be implemented in the module core ... as it's yet.

johnv’s picture

This is a proper patch for itamair's version #22.

philipz’s picture

Basing on #23 I've made a patch to add term mapping by it's GUID. This requires taxonomy terms to be imported by Feeds with GUID assigned to them.

johnv’s picture

@philipz, your patch seems to be built on top of #22. Can you create a new patch which includes #22?
This is common practice, so that the last patch is always the most recent en the 'richest'.

philipz’s picture

@johnv you're right. Here's the patch combined from #23 and #24.

BWPanda’s picture

Status: Needs review » Reviewed & tested by the community

Patch from #26 works for me!

nmc’s picture

Patch #26 worked for me as well. Thanks!

reszli’s picture

#26 works! thanks!

Masala’s picture

#26 patch is applied on dev version perfectly!

GaëlG’s picture

I have last dev version and it failed too. Anyway, it can be applied manually.

GaëlG’s picture

#26 : $target = str_replace(':guid', '', $target); should be at the beginning of the function, no? So that field_info_field() works.

iMiksu’s picture

When this fix gets committed?

philipz’s picture

#32 This might be true :) and checking for ":tid" in the $target should be added as well.

geek-merlin’s picture

Status: Reviewed & tested by the community » Needs work

cool work, but needs work so...

twistor’s picture

This should use the new configuration api for mappers, rather than declare a bunch of targets.
Also, the current mapping behavior should be left alone and deprecated.

For references see:
#1588938: Allow selection of filter for each text field imported
#1697150: Add separator to floats and decimals.

davident’s picture

As a temporary fix against the latest dev:
In mappers/taxonomy.inc Line 91

elseif (is_numeric($term)) {
      /* Temporary fix. Prevents numeric term names from being made a tid.
       * $tid = $term;
       */
      $tid = taxonomy_term_check_term($term, $vocabulary->vid);
}
philipz’s picture

#36 twistor can you please explain a bit more? I checked refereced issues/patches but unfortunately this doesn't tell me much.

sin’s picture

Here is my variant. It adds some options using new configuration api for mappers:
-Term name or ID (old behaviour with is_numeric($term))
-Term name (call taxonomy_term_check_term(), may create terms)
-Term ID (just use $term as tid without additional checks or term creation)
-GUID (use taxonomy_term_lookup_term_by_guid() by philipz, just search, do not create terms)

I tested only GUID variant for importing commerce taxonomy and products from XML via feeds_xpathparser. The patch from #1825682: Notice error correction was used too.

This string from philipz's patch was a mystery for me:
$target = str_replace(':guid', '', $target);
But somehow it works :)

Twistor, please be more specific about "current mapping behavior should be left alone and deprecated", the GUID taxonomy matching feature is absolutely needed for some of our current projects and I might work more on this.

sin’s picture

Title: Split taxonomy mapper in two variants: term name +tid (avoids mapping error for Numeric taxonomy term, too) » Taxonomy mapper options: term name +tid, term name, tid, guid (avoids mapping error for Numeric taxonomy term, too)

I guess we need new title :)

sin’s picture

We might also need to add a key to guid column of feeds_item table.

twistor’s picture

Assigned: Unassigned » twistor

Ok, #39 gets most of the way there. There are some copy/replace errors. This also needs tests.

I'm going to wrap this up tonight.

twistor’s picture

Status: Needs work » Needs review

Alright, here's a go at this.

Thanks to everyone for getting this started.

twistor’s picture

Arg, here's the code.

twistor’s picture

Consistent function naming.

Update:

This changes the existing behavior. I am contradicting myself from above, but after some consideration I don't think many people were using map by tid, and this is still technically an alpha project, so better to just cut loose. This will still be compatible with the vast majority of existing installations.

twistor’s picture

Issue tags: +Needs tests

Tagging

sin’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

It would work for me. Autocreate option is good!

Found a typo in taxonomy_feeds_set_target() -- $cace vs $cache.

sin’s picture

Status: Needs work » Needs review
FileSize
10.42 KB

Fixed typo, tested with GUID option -- works for me, thanks!

ryumkin’s picture

Subscribe

sin’s picture

Related patch to Commerce Feeds #1840198: Add mapping by product GUID.
It has some code from here: select by feeds_item.guid too.

johnv’s picture

Patch #48 gives the following error when importing something without taxonomies:
An AJAX HTTP error occurred. HTTP Result Code: 200 Debugging information follows. Path: /batch?id=977&op=do StatusText: OK ResponseText: Fatal error: Unsupported operand types in feeds\mappers\taxonomy.inc on line 94

Attached patch fixes that:

-function taxonomy_feeds_set_target($source, $entity, $target, $terms, $mapping) {
+function taxonomy_feeds_set_target($source, $entity, $target, $terms, $mapping = array()) {
kovaski’s picture

Subscribe

David Hernández’s picture

Hi there,

I'm having the same issue and I didn't find the issue until I wrote a patch for it. My patch is way less complex and only changes a few lines instead of changing all the functionality of the mapper. What I did is changing the mapper. Before checkinf if it's numeric or a string, I check if the term exists. If so, I import it, if not, I check if it's a number or a string to do the current mapping.

The patch should work and the people will not need to select if they are importing term Data or term ID. But they will have a problem importing Term IDs if they also have taxonomy terms with numeric data.

I will leave my patch here just in case someone thinks it's useful.

Regards,
David.

m.stenta’s picture

Status: Needs review » Reviewed & tested by the community

Patch in #51 works beautifully. Marking RTBC to indicate it's been reviewed. :-)

Status: Reviewed & tested by the community » Needs work

The last submitted patch, feeds-allow-numeric-terms-1019688-53.patch, failed testing.

johnv’s picture

Status: Needs work » Reviewed & tested by the community
FileSize
10.43 KB

@David, no offence, but your patch does the same as #1, and -as you state- still gives errors with numeric/mixed vocabularies.

I'm re-uploading #51, as test-bot apparantly takes the last posted patch, and approved by m.stenta.

westie’s picture

Can I make a suggestion from the UI / UX perspective. I think like how some items have a "target configuration", terms should have options for:

  • Match Term ID's
  • Match Term names
  • Try Match Both

EDIT: ignore didnt look at latest patches

David Hernández’s picture

@Johnv, no ofense taken. I don't like my solution either, I leave it here because it's a quickly solution that can work for some people. At the end, my solution was to use a list field with all the options.

twistor’s picture

Version: 7.x-2.x-dev » 6.x-1.x-dev
Assigned: twistor » Unassigned
Status: Reviewed & tested by the community » Patch (to be ported)
Issue tags: -Needs tests

Added tests. Committed to 7.x-2.x. Thanks everybody.

Frando’s picture

Status: Patch (to be ported) » Needs review
FileSize
679 bytes

Here's a minimal / hackish patch to 6.x-1.x that just does a lookup on tid if the passed value is numeric.

I actually wanted to implement the patch as in #56, but then stumbled: Is it possible to implement configuration per mapping target in 6.x? There seems to be no form callback.

asutton’s picture

Version: 6.x-1.x-dev » 7.x-2.0-alpha8
Component: Code » Feeds Import (feature)

I think I am having a related, but slightly different, issue importing taxonomy terms.

I am using feeds tamper to concatenate two fields (street name and street suffix, ie: 'Main' and 'St') and then save them as taxonomy terms (adding new terms if they do not exist)

The reason I think my issue is related is that when the street name is all letters, ie: Main St, it works perfectly. Hoewever, when the street name contains numbers, ie: 30th St, it adds a new term even if it already exists. So after importing about 15,000 content items, I had several thousand duplicate taxonomy terms, all with 'number' street names.

When I attempted to apply the patch above in #56 it appears the patch has already been applied. I am using Feeds 7.x-2.0-alpha8 and Feeds Tamper 7.x-1.0-beta4.

I have decided to not create taxonomy terms from imported street names, which makes things like making menus from taxonomy terms more difficult, but at least it is working. I wanted to submit this as a FYI as I think there is still an issue regarding importing taxonomy terms with numbers, with the patch above applied. Or perhaps this is a Feeds Tamper issue...

asutton’s picture

Now I am having a similar issue without the use of taxonomy terms.

I am simply trying to use a contextual filter to query my views blocks via the url. When the filter value starts with a letter, it works. But when it starts with a number, it returns no content.

ie: Main St --- good!
7th St --- bad!

Any ideas why this would be? The data was originally imported with feeds into a text field.

I am really stumped so any help is greatly appreciated. Thanks.

asutton’s picture

Scratch the previous post. My problem is that for some reason the streets that start with a number have three spaces in front of it. When I add the spaces to the url, it works. Still not sure where spaces came from....

geek-merlin’s picture

Version: 7.x-2.0-alpha8 » 6.x-1.x-dev
Issue summary: View changes

Please everrrrybody do NOT hijack this issue, but post a new one.
Status of this:
* Committed to 7.x in #59
* 6.x patch for review in #60

geek-merlin’s picture

Version: 6.x-1.x-dev » 7.x-2.x-dev
Status: Needs review » Needs work

EDIT: erroneous comment.

geek-merlin’s picture

Version: 7.x-2.x-dev » 6.x-1.x-dev
Status: Needs work » Needs review

Sorry for the noise.

geek-merlin’s picture

Crosslinking #2139229: Taxonomy mapper creates empty and untrimmed terms which might be of interest for folks here.

twistor’s picture

Status: Needs review » Closed (outdated)