Since exporting nodes flattens the terms
Animals
--Dogs
----Puppy
--Cats
gets flattened to become Puppy and Cats.
When trying to import a node with feeds which has nested terms such as the above, the nested terms will not appear as they do not have parents.
This patch finds and adds all parent terms to the imported node.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Agileware’s picture

Status: Patch (to be ported) » Needs review
Agileware’s picture

FileSize
1.32 KB

Patch for issue.

Agileware’s picture

Title: Import support for nested taxonomy terms. » Import support for nested taxonomy terms. (Patch Attached)
kingandy’s picture

Nice. I just made myself a bolt-on module to do more or less the same thing - though I gave mine a configurable "vocabulary" option so that I could use taxonomy_get_tree, instead of relying on taxonomy_get_term_by_name. I also told it to create a term if it doesn't exist, and only return the TID of the final child term.

Attached in case it's of use to anyone...

kingandy’s picture

Version: 6.x-1.x-dev » 7.x-1.0-beta3

Oh, also, I should note that my module is for 7.x.

kingandy’s picture

Look at me using MySQL functions in PHP

IGNORE ME

7wonders’s picture

Great work kingandy, I personally think this should become a permanent fixture for feeds_tamper. I've been trying to figure this out for a while. I do however pick up a couple of issues with the module. If using the patch for splitting the taxonomy field and then using term name to write to (due to a numerical taxonomy issue) http://drupal.org/node/1019688 , this ends up with the term ID of the end lineage term being added as a new term name and that ID being returned instead (like a vicious circle). Also, if there is an empty field in the array, an emtpy term is added to the lineage, not sure how to handle either of these issues.

7wonders’s picture

If using the patch for numerical terms, changing the line $field = $tid to $field = $name in the callback function does the trick ;)

How to avoid adding blanks if the end of the lineage is empty i.e. 1|2|3|

kingandy’s picture

I think returning $name could get problematic if you have the same term at different places in the hierarchy (which is one of the reasons I needed a firm declaration of parenthood in the first place).

For example if you have
cats|longhaired
dogs|longhaired

Returning "longhaired" will cause Feeds to look for a term called "longhaired", which may or may not be the right "longhaired" category.

(Yes, not a great taxonomy, but technically possible and occasionally unavoidable.)

I think I'd rather advise people that this needs to be done with a "Term ID" feed type. I don't know if it would be possible to put some logic in to make it return $name for the Term Name type and $tid for the Term ID type ... does the Tamper callback have any knowledge of what sort of feed it's operating on?

Regarding the spaces - It should be easy enough to tell feeds_tamper_term_hierarchy_callback to skip blank elements, just add a "if ($name)" to the foreach ($field as $name) loop...

7wonders’s picture

Thanks. I will take a look into the heirarchy issues as I do have a taxo that is possible to have the same term at different places. I will see what it comes out as on my test. As for the blanks, I actually just added a simple find replace regex tamper before the term heirarchy one and that seems to work perfect.

7wonders’s picture

it seems that it works fine using $field = $name in the callback function if using the term name patch i mentioned in #7 and doesnt mess up the lineage if there are same terms in different places. How about putting this in as a patch against feeds_tamper and make it more permanent?

Summit’s picture

Hi,
Could someone make this a real patch? I am on Travel.
That way this can be committed to the module if ok-ed reviewed. Greetings, Martijn

7wonders’s picture

I take back my last comment. It does mess things up as kindandy said and puts the content under the first instance of the term name (even though it registers all the terms correctly in taxonomy).

Agileware’s picture

@kingandy Agree that having multiple terms with an identical name would cause this to arbitrarily pick the first matching term. This is a known trade-off to keep the plugin simple and data import easy. Importing using TIDs whilst precise is not so user friendly.

Perhaps just a note in the documentation is required to indicate that this is the case?

liupascal’s picture

Would it be possible to have the following behavior :
Say the csv file have the following column for a product tagging...
PRODUCTID | PRODUCT CATEGORY
IPOD#4533 | "iPod">"iPod4">"16GB","iPod">"iPod4">"32GB","iPod">"iPod3">"8GB"
(I know the categorization is meaningless, but it's just for this example :-) )

What would be expected here is to have the following taxonomy tree after import :
iPod
-iPod4
--iPod16GB
--iPod32GB
-iPod3
--iPod8GB

We know by using the ">" that the term is one depth further from the previous term. Hence, using the "explode" for instance, we end up with each "iPod">"iPod4">"16GB", then "iPod">"iPod4">"32GB" and finally "iPod">"iPod3">"8GB" terms hierarchy.

If there is currently no term in the vocabulary, for each element, it would go as follow :

Initialize an array of term $terms = array()
1st element : "iPod">"iPod4">"16GB" ("exploded" using php function, then for $i++?)
-> Get "iPod" term with depth = 0, does it exist ? Yes then $terms[0] = "found term" and Continue, if No then create it with depth = 0 and no parent, and $terms[0] = "created term"
-> Get "iPod4" term with depth = 1, with parent == $terms[0], does it exist ? if Yes then $terms[1] = "found term" and Continue, if No then create it with parent = $terms[0], and $terms[1] = "created term"
-> Get "16GB" term with depth = 2, with parent == $terms[1], does it exist ? if Yes then $terms[2] = "found term" and Continue, if No then create it with parent = $terms[1] and $terms[2] = "created term"....
At the end of the for, we return the last element of $terms.

And we go on for the 2nd element....

What do you think of this algorithm ? It also takes care of building the taxonomy tree on the fly, avoiding attaching the a wrong term with the same name

Cheers

liupascal’s picture

Here is another version, *based on #6 kingandy's work*.

I modified the whole parsing function. To test :
- Create a node parser on Article for instance,
- With feed tampers, on the Tags field, use 1/ explode by ',' and 2/ Term hierarchy

The hierarchy must be described in the csv column as it is in the vocabulary. The zip also contains a .csv file you can test with :
column 1 => title
column 2 => GUID (don't ask me why i did this... :-) )
column 3 => Tags

The Tags columns is a coma separated values (for explode) then each "element" is separated by a ">" character.
For instance : "iPod>iPod4>16 GB,iPod>iPod4>32 GB"
Gives 2 terms values -> "iPod>iPod4>16 GB" AND "iPod>iPod4>32 GB"
Where iPod is level 0 depth, iPod4 is level 1 depth and 16GB is level 2 depth.
If you have a iPod of 0 depth tag, it won't get confused with the iPod of level 3 depth tag.

All the terms of the "tree" will be generated on the fly if they don't exist. Else, it will be assigned to it

Pending problem : if there are multiple terms with the same name at the same depth, the 1st one will be used. However, i'm not sure this is actually a problem we can solve if we expect to have term names rather than machine_name in the csv.

You'll find a csv file in the .Zip

prodigeek’s picture

I'm having trouble getting this module to work. I've loaded Tamper with the Hierarchy field pointing to the correct Vocabulary. I include the hierarchy tags with a > in between them like this: Marvel Comics>Danger. But when I run the import, a new tag is created with Marvel Comics>Danger as one single term. I tried adding an explode command before the Hierarchy, but that just prevented the taxonomy from loading at all.

I've been looking for a module like this forever. Thank you for all your effort and time.

Michael

liupascal’s picture

@prodigeek,

Are you trying my module fork or kingandy 's module ?
Try using my sandbox http://drupal.org/sandbox/liupascal/1382116

7wonders’s picture

I use the module in post #6 and it works great. Use any unique string to explode on (e.g. I use tilda as my file is something;something2;something3;a~b~c~d) then followed by term heirarchy plugin.

So you end up with an explode (~) followed by the term heirarchy. This gives the heirarchy a>b>c>d.

Summit’s picture

Hi, Could this plugin may be be connected to xpath parser, as such that the hierarchy can be build after using the xpath parser, and then feeds tamper takes over and makes the right hierarchy please?
greetings, Martijn

prodigeek’s picture

I'm using the most updated version of your module. When I set Explode and the Hierarchy in Feeds Tamper, the feed import does not work. When I try the Hierarchy alone or the explode, it also does not work. I had it working for a bit with an older version, but it would not create new terms in their proper hierarchy, it would create them at the top level. What am I doing wrong?

I'm so excited for this module to work. I have several thousand taxonomy terms this could help organize.
Thanks.
Michael

AaronELBorg’s picture

EDIT:

Make sure you're allowing multiple values in your content type and not just grabbing the deepest term. (You want all terms.)

That was my problem a couple minutes ago anyway.

AaronELBorg’s picture

Anyone having issues with numbers in a taxonomy, see this issue (and patch):

http://drupal.org/node/1426762#comment-5612248

ret5’s picture

Hi all. I'm using the module from #6 and looks like it might do the trick.
Question: I have setup the explode and hierarchy in feeds tamper but not really sure how to configure the mapping in feeds. Can anyone walk me through that? I have a csv file with a field called "combined" where i've dropped my comma separated terms, for example: Product1,Detail1,Detail2

I've currently setup mapping to source only the "combined" field from above, then, set the target as "temporary target1". Tried importing on that alone and it sorta worked... resulted in lots of errors saying it failed import however when I pull the taxonomy listings, there are the terms as expected broken out by hierarchy. Is this a fluke of this approach or am i still going about this wrong?

Thoughts / advice appreciated.

Update: Actually the error message received is "Term name missing" followed by "Failed to import."

2pha’s picture

I was trying liupascal module but the problem he outlined...
"
Pending problem : if there are multiple terms with the same name at the same depth, the 1st one will be used. However, i'm not sure this is actually a problem we can solve if we expect to have term names rather than machine_name in the csv.
"
was a problem for me as I had many of the same terms under different parent terms.
I also found that I needed to add an explode before the term hierarchy tamper thing.

I rewrote it based on liupascal's sandbox and addressed both problems and it is working for me.
No guarantees it will work for anyone else though :)
zip attached

pindaman’s picture

#25 creates teh tree i need. GReat stufF!
thanks

Strompf’s picture

How do you apply these modules?

I'm using:

  • feeds-7.x-2.0-alpha5
  • feeds_tamper-7.x-1.0-beta3
  • Feed Tamper Term Hierarchy from #6
  • Custom Feeds Tamper Term Hierarcy from #25

Catalog:

MainTerm01
---Subterm01-01
---Subterm01-02
MainTerm02
---Subterm02-01
---Subterm02-02

Import file (Tab-separated with only one node):

GUID SKU Title Catalog
01 01 Title-01 MainTerm01|Sub01-02|MainTerm02|Sub02-01

When I use Explode + any of the two Tamper Term Hierarchy plugins, no taxonomical data gets processed.

When I only use the Tamper Term Hierarchy plugin, no taxonomical data gets processed (makes sense: no separation symbol has been specified)

When I only use the Custom Tamper Term Hierarchy plugin, all taxonomical terms are stringed into one taxonomical path

Any help is greatly appreciated!

2pha’s picture

you do not need both #6 and #25, only #25

Strompf’s picture

Hi 2pha,

thanks for replying. I didn't use them at the same time, of course.

Do you maybe have some screenshots, or an example of how you got this working?

David_Rothstein’s picture

This issue seems to be going in several directions at once, with a bunch of different patches - or in most cases, zip files :)

I would say that exploding multiple terms from a string separator is out of scope for this issue (there is already an Explode plugin in Feeds Tamper which does that), as is auto-creating new terms (the Feeds module already does that on its own by default; in fact, you need something like #1416700: Add plugin to limit input to words/phrases matching existing taxonomy terms in order to prevent that).

I needed this feature too, so I tried to write something simple that would work, and the attached patch is what I came up with. I think it's closer in spirit to the original patch that was postd in this issue. Here, you input either a string (a single term name) or an array of term names and get back an array of term IDs representing the term hierarchy. It also correctly handles terms with duplicate names that occur in different places within the hierarchy (see the code comments for more details).

As mentioned, the plugin returns an array, but if you need to get that down to a single term (e.g., for storing in a single-valued field) I recommend combining this with #1819986: Add a plugin for reducing an array to a single element which I just posted as well.

The one problem I'm aware of with this patch is that due to the way the duplicate terms and IDs are handled, it winds up preventing Feeds from creating new terms like it normally would (i.e., it essentially has some of the functionality of #1416700: Add plugin to limit input to words/phrases matching existing taxonomy terms built in). So if you need to be able to create new hierarchical terms on import (rather than just importing into an existing vocabulary) then this patch won't work for you. Ideally the patch would be written in a way that didn't prevent Feeds from auto-creating terms (and then you could put the Feeds Tamper plugin from #1416700: Add plugin to limit input to words/phrases matching existing taxonomy terms after it if you needed that restriction as well), but I wasn't sure how to do that, and it met my own needs in its current format so I'm posting it as is.

Status: Needs review » Needs work

The last submitted patch, feeds-tamper-term-hierarchy-1319278-30.patch, failed testing.

David_Rothstein’s picture

Status: Needs work » Needs review
David_Rothstein’s picture

Title: Import support for nested taxonomy terms. (Patch Attached) » Support automatic importing of the term hierarchy for nested taxonomy terms
Version: 7.x-1.0-beta3 » 7.x-1.x-dev
Strompf’s picture

Version: 7.x-1.x-dev » 7.x-1.0-beta4
Category: feature » support

I still don't get it to work. I'm using the Explode plugin followed by #25.

Hierarchy (already present):

All
Interior > Blue
Interior > Red
Exterior > Blue
Exterior > Black

Node information (Ubercart product):

GUID	SKU	Title	Catalog
01	01	Car-01	All|Interior>Blue|Exterior>Blue

I've used different combinations of separators, but as soon as I use #25, no taxonomical data gets imported with the article. Actually, I don't understand the documentation for #25 at its configuration page:

This will use an array of strings to build a tree of terms. For example, array("a", "b", "c") will form a hierarchy of terms with "a" as the root and "b" as a final child. The term ID of the ultimate descendant ("b") will be returned.

I also don't understand what the 'Term hierarchy separator' is used for. Doesn't #25 just turn an array into a tree according to the indicated hierarchy?

Thanks already!

David_Rothstein’s picture

Version: 7.x-1.0-beta4 » 7.x-1.x-dev
Category: support » feature
zeezhao’s picture

In relation to #34, I did get it to work with ubercart (7.x) using latest feeds_tamper and feeds dev modules and custom_feeds_tamper_term_hierarchy from #25. However I had only one hierarchy, so what I did was to have the Catalog as a field on the ubercart importer with an entry like this to use your example: "All>>Interior >> Red"

Sounds like adding to multiple hierarchies is a feature request: I also tried is having another field "Catalog2", and map this field also to Catalog, and then have "All>>Exterior >>Blue" in that field. Feeds does allows multiple mappings. But this meant Catalog2 over-wrote Catalog after product was loaded.
[in your case: GUID, SKU, Title, Catalog, Catlog2, ... ].

2pha’s picture

It was a while ago but I don't think the module in #25 supports an item being in multiple taxonomies by itself. When I made it I did not need that functionality.

Strompf
"I also don't understand what the 'Term hierarchy separator' is used for"
The separator is the character that separates your taxonomy terms.
eg. 'cars|honda|civic'. in this case the pipe character '|' is the separator.

The problem you are having seems to stem from the fact that you are sending the string 'All|Interior>Blue|Exterior>Blue' which seems to have multiple category trees. You should first extract the separate taxonomy trees. eg. 'All', 'Interior>Blue' etc with an explode using the pipe '|' as the separator/delimiter and then use #25 with the separator set to greater than '>'.

I am writing this without testing it, so no guarantees.

algazaras’s picture

Hello there,

First of all thanks a lot for this plugin, it is really useful.
I have a question, though. I have a multivalue field in my content type where I want to enter two taxonomy terms. I am using the rewrite, the explode and the custom term hierarchy plugins, but I get a "hash" term, and not the two terms I would like.
I have two mappings from my csv file: term 1 and term 2.
I rewrite like this: 'Level 1>>Level 2>>[term 1]','Level 1>>Level 2>>[term1]>>[term2]'
I explode with a comma as separator.
I use the term hierarchy plugin.

I have tried with quotes and double quotes, but I can't get it to work. Anyone can help me please?

2pha’s picture

OK....I needed this functionality again, So I downloaded the zip file I attached here last year. Except this time I needed to support multiple terms.
eg.
A column in one of the rows of a csv file I was importing had taxonomy as
'Holden>>Commodore>>VN,Nissan>>Silvia>>180sx'.

The previous version would not allow for this, so here is a new version that will allow for multiple terms.

To use it add an 'explode' with ',' as the string separator.
Then add (Custom) Term Hierarchy with '>>' as the Term hierarchy separator.

If I ever get the chance I will create a patch for the feeds tamper module..

2pha’s picture

The last attachment had many 'dpm' calls left in it (and I cant remove the attachment). So here is a version without all the annoying dpms.

Summit’s picture

Hi, thanks for the plugin. Couldn't this plugin be placed in the module?
So it can be maintained?

Greetings, Martijn

2pha’s picture

Sure, create a patch.

devprocess’s picture

Does this work with hierarchical select? I am using the custom feeds tamper term that 2pha developed on #40 to import multiple terms from a csv, but it doesnt seem to work. The hierarchy I am using is tagging vehicle parts to vehicles in a Make/Model/Engine/Year tree. Any feedback would be greatly appreciate. Thank you.

2pha’s picture

@devprocess
The zip I attached has nothing to do with hierarchical select, It simply imports taxonomy branches. It should work fine with a Make/Model/Engine/Year set up. I created it because I needed Make/Model/Series for vehicles.
If you tell me what sort of trouble you are having when importing, I may be able to help.

devprocess’s picture

So it turns out they restructured the taxonomy hierarchy and we are receiving this error:

Trying to get property of non-object in hs_taxonomy_hierarchical_select_valid_item()

I found that it seems to be an error with feeds and there is patch for it. I am going to work on this error, and possibly this is what is stopping the hierarchy import. When I would run it normally I would receive a product updated response, but when I would look at the product or try and filter it would not have any data. Thank you for the reply, I will check now and see if this will get it to work.

devprocess’s picture

The error was fixed by patching my hs_taxonomy.module to this version:

http://drupalcode.org/project/hierarchical_select.git/commit/22acf869782...

I ran the feeds import using the same setup you posted on #39 and it did add terms, but instead of:

Volkswagen>>Cabrio>>2.0L l4 GAS SOHC NA>>2000,Volkswagen>>Cabrio>>2.0L l4 GAS SOHC NA>>2001

being the term lineage, what was saved was:

24977,24978

Rather confused.

devprocess’s picture

Upon investigation, it seems to be saving the tid's of the table.

2pha’s picture

That's weird.
The only thing I can think of is that the term reference field that it is importing to is set to an autocomplete textfield (instead of a select). The textfield expects a term where a select expects a term id. The tamper term hierarchy thing returns a term id.
Make sure the term reference field you are importing to is a selectbox / combobox.
This is the only reason I can think of that may be causing your problem.

devprocess’s picture

Interesting. I asked around and apparently the previous person in charge of this project patch feeds with this:

http://drupal.org/node/1426762

I set it back to to the original file, but am now receiving and AJAX HTTP error. Clearly there is something incorrect here but it seems your Custom Feeds Tamper is working correctly, just something is wrong in Feeds, Commerce Feeds, or with our Server. Thanks for all the help!

nitrospectide’s picture

I am running #40 and it works magnificently. Thanks 2pha!

For anyone still unsure how this works (this thread was hard to follow in places), I will lay out what I'm doing.

My CSV is this:

GUID,title,body,taxonomyterm
101,History,,company>>about
102,Management Team,,company>>management
103,Board of Directors,,company>>management

I am importing these to Basic Page nodes. The "taxonomyterm" field is mapped to a field on my Basic Page content type that is connected with a taxonomy vocabulary. Prior to import, the vocab is empty.

On the Mapping page under Processor in my Feed Importer, I click Configure Feeds Tamper at the top of the UI in order to tweak how the taxonomyterm field imports. I find that field in the list and add a plugin of type "Custom Term Hierarchy". I select my taxonomy vocabulary I'm connecting with in the dropdown, and then leave the ">>" as the term hierarchy separator. Now, when I import from my CSV, the taxonomyterm field does one of two things: If the term exists, it just uses it and selects the proper term on the node (note 2pha's comment at #48 where the field on your content type must be a dropdown and not a text field), or if the term doesn't yet exist, it is created. This means that in my case where I start with an empty vocab, the CSV builds the pages and taxonomy vocab simultaneously!

NOTE: There seems to be an issue between the current versions of Feeds and Feeds Tamper. I had to roll back to a previous version of Feeds. http://drupal.org/node/1929312

nitrospectide’s picture

2 requests:

Could someone please roll this into a patch so that it becomes part of Feeds Tamper?

Could the settings in the plugin be reworked to be savable via Features? I saved my importer as a Feature, and everything but the plugin settings came through.

David_Rothstein’s picture

I posted a patch in #30 earlier, and it's up for review.

As mentioned, it's missing the ability to add terms, but like I said there, having the Feeds Tamper plugin force-add them (like #40) doesn't seem like the right way to go either? (The plugin really shouldn't interfere with the normal way in which terms are processed.)

nitrospectide’s picture

I'm not familiar with how things are architected under the hood, but would definitely need to have my terms get added if not already present. What are you recommending as far as how terms get handled if not present? Is there a way to have Feeds just add them when they are passed in as a hierarchy?

2pha’s picture

@nitrospectide. The module I posted at #40 adds them if not present. I am unsure why David_Rothstein does not think this is the correct way to do it.

David_Rothstein’s picture

Like I said above, Feeds already adds them when the actual import happens.

In my opinion, the goal here should just be not to do anything to get in the way of that. (It's been a long time since I worked on it, so unfortunately I can't remember why it didn't happen naturally in this case.)

The purpose of Feeds Tamper as described in the first sentence on the project page: "Feeds Tamper provides a small plugin architecture for Feeds to modify data before it gets saved."

It's supposed to allow many independent plugins to be mixed and matched together to massage the data. But if one interrupts the process and saves things on its own, that seems like a problem.

nitrospectide’s picture

The gist of this thread though, is the idea of exploding one field into multiple values. Is there any provision in Feeds to pass the multiple values up the chain and have Feeds save them? Is it maybe only expecting to receive one term, and to have it receive and save two, it would need to be reworked?

chrisjlee’s picture

Is there even a patch that works that someone has tested?

2pha’s picture

@chrisjlee no there is no patch, and there probably wont be for the reasons that David_Rothstein rightly outlines. I may create a module providing the functionality in the future, but don't count on it. Until then there is no reason why you cant use the module I posted in #40

Funksmaname’s picture

Hey 2pha,
Thanks for your plugin #40 - I just have a query...
I'm getting no errors, and the taxonomy is being correctly imported to the vocabulary - hierarchy and all. However, for some reason, it isn't actually being selected for each node?

I've set the widget to be 'select'. I am just using a single test entry with a single term (e.g. fossils>>old>>rock)

Going to the imported node now shows this term hierarchy as a selectable option in the field, but it wasn't selected during import.

Any advice greatly appreciated!

-------

NVM! (fixed)

It appears the mapper for the term was set to search 'term name' not 'term ID' - this was sort of implied in the thread, but wasn't identified as a required configuration change from default value when setting up the mapper. Hope this helps someone and THANKS AGAIN!
FYI, the 'add term' check box should NOT be ticked (it's added anyway) otherwise funny number terms are also created.

RAFA3L’s picture

Hello,

Finally all is working fine, except for one thing, the taxonomy term field of my node is filled only with the last term of the chain, and I would like keep all the lineage with the Hierarchy Select module, this is possible?

nasia123’s picture

I confirm that #40 works
Feeds->7.x-2.0-alpha8+7-dev
Feeds Tamper->7.x-1.0-beta4

nasia123’s picture

Also ,mapping for term should be 'term id' , in order for this module to work with the latest versions of feeds an feeds tamper module.

sensifreak’s picture

I cant confirm that linage is working with this combination. Is every term in the hierarchy selected in your installation?

Is there any solution for hierarchy AND linage?

Best regards Julian

pdesai’s picture

@nasia123: Thanks for the clarification of using "term id" instead of "term name" to help with our import.

Same request as @sensifreak - is it possible to associate not only the child term, but also all of the parents?

2pha’s picture

I'm not sure what you mean by lineage or associate the parents. Isn't that what hierarchy does?

sensifreak’s picture

It should work like this. But it doesn't work :)

pdesai’s picture

An example of what we are asking is this. Let's say the term structure is Make >> Model for cars taxonomy. The term example could be: Honda >> Accord. Your custom module works great with Feeds and associates the node to the term Accord which is a child of Honda which solves the hierarchy issue. The lineage issue that we were requesting is that we would also want this node to associate to Honda term. So if we have a view of all Honda nodes, this mode will be listed. And if the user drills down to find all Accord car type it would be listed as well.

Hope this helps.

2pha’s picture

This could be done with a view, you would not need to associate the node
with both honda and accord.
If the node is just associated with accord, and accord is a child of honda,
a view can be created to display the accord node when honda is selected.

pdesai’s picture

Yes, I know the view can be setup to show child of parent terms. However, when a user clicks on the taxonomy term Honda and it goes to listing of /taxonomy/honda/ which wouldn't list the node because it isn't associated with that term.

There https://drupal.org/project/hierarchical_select module that allows for this where you can set the term reference field on the node to associate with all terms (not just the lowest child). Which works great if you edit a node individually and select the term Honda > Accord - it will assign to both. However, even though the term reference field is setup to do this, when using Feeds and Feeds Tamper with this custom module - it only associates with the lowest child term.

Hopefully this clears it up. Maybe there is another way to handle this without associating to the entire lineage.

Thanks in advance!

2pha’s picture

"when a user clicks on the taxonomy term Honda and it goes to listing of /taxonomy/honda/ which wouldn't list the node because it isn't associated with that term."

There should be a default view that you can enable that will override the taxonomy term pages with a view and from there you can change it to accomplish what you want.

I have done this on many sites (including the one I made this code for) as I needed the same functionality as you. It was also for car makes and models.

pdesai’s picture

Thanks! I was using the examples of Make/Model for easier reference compared to actual terms we use since they are industry specific. However, I was able to get it working based on your comment.

For anyone that needs to show nodes associated with child terms in a view using Drupal 7 + Views 3 - update the Contextual Filter using Content: Has taxonomy term ID (with depth) and there is an option called Depth that you can set to how many tiers are in your hierarchy to show. For example, we have a parent, child, grand child, great grand child (four tiers) so we set the Depth dropdown to 4 since it starts at 0 (which means only show current term's node).

Thanks again 2pha!

2pha’s picture

Glad to hear you got it sorted :)

sensifreak’s picture

I found another solution for my problem. Using views isn't an option for me because i use faceted search with facetapi.

My workaround is:

  1. import the term tree with Taxonomy CSV Import / Export.
  2. In feeds i just use the deepest term in the hierarchy.
  3. My custom module saves all parent terms of the selected term with hook_node_presave()

function MYMODULE_node_presave($node) {
  // Get the taxonomy terms.
  if ($terms = field_get_items('node', $node, 'MYTERMREFERENCEFIELD')) {
    $tids = Array();
    foreach ($terms as $term) {
      // Add the term to a list of tids.
      $tids[$term['tid']]['tid'] = $term['tid'];
      // Get the parents of the taxonomy term.
      $parents = taxonomy_get_parents_all($term['tid']);
      foreach ($parents as $parent) {
        // Add the parents to a list of tids.
        $tids[$parent->tid]['tid'] = $parent->tid;
      }
    }
    // Add the list of tids to the node.
    $node->MYTERMREFERENCEFIELD['und'] = $tids;
  }
}

nasia123’s picture

#71 works , nice tip!

nasia123’s picture

Issue summary: View changes

clarifying issue

Robin van Emden’s picture

2Pha, many thanks for your great module at #40 - saved me many many hours of work!

axejko’s picture

Issue summary: View changes
FileSize
2.61 KB

Исправлена дырка - дублирование и повторное создание терминов, в том случае, когда в строке между разделителями попадаются пробелы

Summit’s picture

Hi @Axejko, please state in english...
Thanks!
Greetings, Martijn

axejko’s picture

Да пожалуйста))

Fixed hole - duplication and re-creating terms, when in a row across the gaps between the delimiters

casper83’s picture

Thanks for your effort axejko
bu i have a problem with importing with multi level here is the example:

guid,name,description
5399001,RECat One,Some text description for tax
5399002,RECat One>>SubRECat 1,Some text description for taxtwo
5399003,RECat One>>SubRECat 2,Some text description for taxthree

first: it gives me this message

Warning message
Term name missing.
Term name missing.
Term name missing.
Status message Failed importing 3 terms.

second: it imports the terms with the desired hierarchy but with out GUID & with out description

third : in the log of Feed it consider no items add so if i wanna delete last added i have to do from the taxonomy manager not feed log.

is there solution for this bug !?

2pha’s picture

The modules included in this thread have nothing to do with your guid or description and should only be acting on the 'name' column.

casper83’s picture

Yes 2pha , i know but when i add (Custom) Term Hierarchy to the term name it add the terms in hierarchy well but because it throws exceptions for some reason, the other fields (guid, description) can't be added , but if i disabled the custom term hierarchy they are saved well as normal.

2pha’s picture

Ahh, ok. Which version did you use? I am not sure about the version in in #76 as I have not tried it. Try using the one in #40 as many people have used it and no one has reported this problem before.

Strompf’s picture

After the usual bit of struggling, I'm glad to report that I got it working!

Some examples: devliegendebrigade.nl/wiki/Custom_Feeds_Tamper_Term_Hierarchy-module

Although in Dutch, the screen copies are all in English. I hope it is of value to anyone.

Regards,
Jeroen Strompf

ganbat’s picture

Hi 2pha.

I'm trying to use your module (#76, because #40 is duplicating terms). My node CSV is following:
title, body, model>>color

taxonomy term is:
iPhone>>Black
iPhone>>White
Samsung>>Black
Samsung>>White

I tried your module it creates taxonomy terms, but doesn't attach terms to my node.
Could you advice some instructions please.

Thank you.

sterndata’s picture

I'm tried the version in #40 and get the same error as #49, An AJAX HTTP error occurred. HTTP Result Code: 500 Debugging information follows. Path: /batch?id=33&op=do StatusText: Internal Server Error ResponseText:

so, I tried using #76 and it does create the nested taxonomy terms, but it seems to attach a number, not the terms, to the object being imported, not the terms and it creates that number (in this case, "84") as another term in the same vocabulary.

The CSV test file and feed export are posted at http://pastebin.sterndata.com/pyy0ywrwr

lmeurs’s picture

The only difference between #40 by 2pha and #76 by axejko is that the latter removes white spaces from the string containing the taxonomy terms, ie. " Colors >> Red " (notice extra spaces) becomes "Colors>>Red".

I also had some trouble with this plugin and decided to rewrite the latest version. The rewrite uses the original field's vocabulary (no need to set it at the plugin settings page again) and respects the "Auto create" setting.

Our import file is similar to:

id;tags;title
ABC001;"Category 1,Sub-category 1";Product 1
ABC002;"Category 1,Sub-category 2";Product 2
  1. Use Feeds' Node processor (have not tried the Taxonomy term processor).
  2. At the mapping settings set "Search taxonomy terms by" to "Term name" and
  3. Check "Auto create".

NB: By default Feeds only handles new or altered data, so when testing the same file over and over again be sure to check the "Skip hash check" checkbox!

EDIT: In hindsight a few things got more clear. When testing the plugins from #40 and #76 I set "Search taxonomy terms by" to "Term name" which seems logical since we provide term names, but to keep hierarchical order the plugin returns term id's to Feeds and thus the setting has to be set to "Term ID". Now both plugins work as advocated, the plugin from #76 probably slightly better since it cleans up white spaces. I will disable my previously attached rewrite of the plugin.

Though the plugins work great and I really do appreciate the work, the approach seems kind of hacky because:

  1. This functionality should probably not be offered from a Feeds Tamper plugin. Feeds Tamper is (correct me if I'm wrong) only to alter data before it is being processed by a Feeds processor.
  2. Though the field is already linked to a vocabulary, the vocabulary has to be set for the plugin as well.
  3. Using the "Search taxonomy terms by: Term ID" settings make things confusing.

Especially because of the first reason this plugin will never be committed. I looked for a mapping method for the Node processor which offers the same functionality and might have found a solution, see my next comment.

lmeurs’s picture

Status: Needs review » Active
FileSize
2.93 KB

As I said in my previous comment I looked for a mapping method for the Node processor which offers the same functionality as the plugins offered in this thread and found a sandboxed module at https://drupal.org/sandbox/manarth/1540722. Though only a sandbox project and not compatible with the latest Feeds (#2021519: Feeds 7.x-2.0-alpha8's compatibility), it really looked like a better approach so I combined this module with the original mapping from feeds/mappers/taxonomy.inc.

I attached the result module:

  1. Enable the module;
  2. Add a mapping for your taxonomy field and select ie. "Tags: hierarchical by term name";
  3. Now you can optionally set the separator as a map setting;
  4. You can optionally use Feeds Tamper to explode a multi value field.

Succesfully tested on an import file similar to:

id;tags;title
"ABC001","Product 1","Category 1>>Sub-category 1|Category 3>>Sub-category 1"
"ABC002","Product 2","Category 1>>Sub-category 2"

Since the functionality of the plugins seems out of scope of the Feeds Tamper module, I thought it was a good idea to change the thread's status from "Needs review" to "Active" so new visitors do not start looking for patches. Closing the thread might be the best, but that's not up to me.

lmeurs’s picture

MegaChriz’s picture

casper83’s picture

Thanks for your support
but its not working
it imports it "cat1,cat2,cat3" as 1 taxonomy !

R.Sungatov’s picture

Great! Saved a lot of time for me! I migrate galley2 with more than 80000 pics into my Drupal installation. Albums interpreted as hierarchical taxonomy terms

vbard’s picture

#76 worked great!
It worth mentioning you must set "Search taxonomy terms by: Term ID" in field mapping settings as plugin returns tid.

Summit’s picture

Looking forward for #76 in Drupal 10! Greetings,