I know that there are some modules which allows to create taxonomy terms based on node info; such as NAT - based on node title and Content Taxonomy - based on words entered to specific taxonomy fields. However, what i'm looking for is a system which will assign existing terms to existing nodes based on some IF-THEN logic.
Let's say i have a CCK content type C, with several integer fields F1, F2 and F3, each ranging from 0 to 100. I have also a vocabulary for content type C with terms T1, T2, and T3. And finally, there are already existing C type nodes N1, N2, N3. Now, what i want to make is to assign terms to existing nodes based on F1, F2 and F3 values. For example;
For all nodes
--- if F1>70, assign T1 to Node
--- if F2>70, assign T2 to Node
--- if F3>70, assign T3 to Node
In the real world, there may be some situations where this functionality will become very handy. Let's say you have a product db with thousands of products and each product has a price field. You want to group products into some price ranges such as "lower than 1euro, 1-100 euro, 100-1000 euro and 1000+". Assigning these terms manually would take a lot of time.
First, i want to know if this kind of functionality is possible using the existing modules. And if it is not, then how can this be accomplished?
Comments
I wrote a very basic module
After a few hours of work, i was able to achieve this functionality. I'm neither a php/mysql expert nor an experienced Drupaller, so the code is possibly ugly and not very flexible. Nevertheless, I'll share the code, in case someone might want to work on it and develop a more flexible module. In fact, that would be a great module especially when coupled with the Faceted Search module.
First, i want to explain what this module does and where you can use it.
- You have a custom CCK content type with contains some integer CCK fields.
- You also have a vocabulary of terms for this content type, which you want to assign to nodes based on those CCK field values.
- You want to assign a term to a node if a specified CCK field has a value greater than a given threshold value for that term.
So basically you have:
Integer CCK fields: F1, F2, F3,
Terms: T1, T2, T3,
Threshold Values: V1, V2, V3,
Relations: (T1,F1, V1), (T2, F2, V2), (T3,F3,V3)
And you want to assign terms as follows:
FOR ALL NODES
--- IF Node's F1 Value >= V1 THEN ASSIGN T1 to this node
--- IF Node's F2 Value >= V2 THEN ASSIGN T2 to this node
--- IF Node's F3 Value >= V3 THEN ASSIGN T3 to this node
So this module takes all those information and assigns terms to nodes. Since the code isn't very flexible, you have to edit the module file for each vocabulary and content type. The user interface is very basic; there are two options in the module menu: one for deleting all the term assigments of the given vocabulary (don't forget to backup your db!), and the other for term assignment. You have to enter all the required data directly to the module file. And also don't forget to add a menu link for the autoterm page, since it doesn't add one automatically.
And here's my ugly code :)
Installation
1. Copy/paste this code as autoterm.module, and also add the following code as autoterm.info file.
2. Create a folder named "autoterm" and put both files in this directory and move it to the drupal modules directory.
3. Enable the module in admin->modules.
4. Go to Access Control and set permissions as you want.
5. Open autoterm.module file with a text-editor and enter your own data.
6. Go to "yoursite.com/autoterm" page, select "Assign terms based on CCK fields" and click "Process Nodes".
Would be great if this
Would be great if this becomes a module on drupal.org! with the flexibility and UI. greetings, Martijn
I'm working on a module to do this with full config in drupal
I'm working on something like this (but not for CCK) that will be fully configurable from within Drupal. Mine updates the taxonomy during the forms submission (add or update). One of the things I'm missing is what to do if they change the cutoffs. But I think using your code will get me so I can then update all of the nodes at once.
I'll get the code out as soon as I integrate your code.
Dave
Auto Taxonomy Fields
Here it is.
README.txt
taxbyrange.info
taxbyrange.module
I actually didn't end up using your code.
Dave
Hi Dave, thanks for the
Hi Dave, thanks for the great work. Will you provide this as a module for Drupal (i mean in the modules directory)?
Module
Since there seems to be some interest I'll try and get it posted as a module.
Dave
This is great, i'm looking
This is great, i'm looking forward to using it in my next project :)
great + thanks momper
great + thanks
momper
Has anyone been able to get
Has anyone been able to get this working? I implemented as directed but it doesn't seem to be assigning the taxonomy terms.
sorry, double post.
sorry, double post.
Update to get it to work with CCK fields
Change the part that has:
// The field in question has a value
$fieldval = $node->$params[1];
to be
// The field in question has a value
if (!is_array($node->$params[1])) {
// Non CCK the value is held directly in the node element
$fieldval = $node->$params[1];
} else {
// It's an array so assume CCK
// Get the field element
$fieldElement = $node->$params[1];
// Get the value
// CCK stores the value in an array along with a display version of it - we just want the value
$fieldval = $fieldElement[0]['value'];
}
You can all thank jimi089 for getting me to update it for his project.
Dave
module?
Hi,
I really like that functionality. Did you manage to get it in a Drupal module? Is it D6 compatible?
cheers, dom
-=-=
This looks very interesting, I would very much welcome such functionality in drupal 6.