Greeting,
Please help, because I know that many people are looking for a solution to something like this, while there is no helper module.
Here's the scenario:
I have a site where people submit URL links to external articles, powered by Drigg module.
I want to take the full URL address submitted in the $form['url'] field, strip away the http:// or http://www. , strip away everything that comes after the .com and then, take the new clean word example.com and store it in the database as a taxonomy tag. In effect, I hope to have taxonomy tags which pull together all the submitted articles from a given external domain.
To achieve this, I'm trying to use the Computed Field module, which allows us to enter PHP snippets and calculate a new field from values entered in another field.
$url is something that comes from the Drigg module, and it's the full external URL of an article
So far I know how to strip away the http:// or http://www. and everything that comes after the .com with this code:
$myurl = preg_replace('%^(https?://[^/]*).*%', '\1', $url);
$mydomain = str_replace('http://', '',str_replace('http://www.', '', $myurl ) );
which works fine in my theme. Then...
On this handbook page http://drupal.org/node/297030 I have found a PHP snippet that claims to be able to write a field to a vocabulary, but this seems to be for 5.x. So far I have:
<?php
$vid = 2; //this is the ID of my Tags vocabulary
$escaped = check_plain($node->url['value']);
$existing_terms = module_invoke('taxonomy', 'get_term_by_name', $escaped);
if (count($existing_terms) > 0) {
foreach ($existing_terms as $existing_term) {
if ($existing_term->vid == $vid) {
$term_id = $existing_term->tid;
}
}
} else { //the term exists
$edit = array('name' => $escaped, 'vid' => $vid);
taxonomy_save_term($edit);
$term_id = $edit['tid'];
}
// associate node and term
$node->taxonomy = array($vid => array($term_id => $term_id));
?>
But no new taxonomy terms are being generated :(
I think the problem could also be in $node->url['value'] -- I'm not sure if this is the correct spelling of it. Somehow I need to make it into the value deduced in $mydomain
Can anybody help and fuse the two snippets together into a working code? (You'll be helping a large number of drupal users who have been looking for a working solution to this)
Many thanks!
Comments
Saving taxonomy terms is easy
Saving taxonomy terms is easy :).
Here is code for 6.x:
---
Yuriy Babenko | Technical Consultant & Senior Developer
http://yuriybabenko.com
Hi Yuriy, thanks a lot for
Hi Yuriy, thanks a lot for helping.
But I'm not sure I understand. Where in my code should I put your code? Can you post the full code please? And most importantly, which $ should I put in 'My new term' in your code?
thanks!
I'm not familiar with the
I'm not familiar with the ComputedField module you're using, but I'd guess that's where you will want to put this code. Right where you have
You would add the code I posted:
This would save a new taxonomy term ($mydomain) in VID (vocabulary ID) 2. Note that you'll probably want to check to make sure this term doesn't already exist (prevent duplicates). You may also want to assign this term to the node you're working with. These two steps are a little more involved, but not too complicated.
Personally, I'd do all of this work in a new module. Just create a simple module and implement hook_nodeapi(), placing all this code in the appropriate section within.
---
Yuriy Babenko | Technical Consultant & Senior Developer
http://yuriybabenko.com