Posted by Sepero on October 21, 2010 at 2:38pm
I'm sorry if this seems stupid, but I can't figure this out for the life of me. How do I test for the existence of a term?
After scouring the internet for hours, some people referenced Textual Comparison and [node:term] as their solution, but none actually said how. I only seem to get the first term when using [node:term].
Comments
One of the shortcomings of
One of the shortcomings of tokens is that it only returns the first element in an array / list. So if you have a list of terms and you use [node:term] it will only give you the first.
I believe you could use PHP to pull that array and check for the existence of the term.
-
@foggyperspectiv | foggyperspective.com (blog)
Wow... That really sucks for
Wow...
That really sucks for me since I have no idea how to do such a thing.
At least now I can stop wasting time trying to do the impossible. Thanks for letting me know caschbre
Thanks to some help from a
Thanks to some help from a follow user, this php code can be entered as a conditional test. Change 'lower_case_term' to the term you wish to match.
foreach ($node->taxonomy as $term) {if (strtolower($term->name) == 'lower_case_term') {
return true;
}
}
return false;
Thanks again caschbre.
Also, this can work, if you
Also, this can work, if you know the specific number associated with the taxonomy term. Replace '1' and '2' with appropriate numbers.
return array_key_exists('1', $node->taxonomy) and ($node->taxonomy['1'] == '2'));Find taxonomy numbers by running the rule with this code, then look at the webpage source.
print_r($node);Where to enter?
This seems to be the perfect answer to my question but can somewhere tell me where you can enter the code? Are we talking about the rules module or the conditions module?
Thanks.
This PHP code is entered in
This PHP code is entered in Rules.
site.com/admin/rules
Create a new rule using, Event "Content is going to be saved".
You will want to "Add a condition", and choose "Execute custom PHP code".
Custom PHP Option
I'm not sure why but I don't have an "Execute custom PHP code" although I can see how useful that would be. Any idea why I'm missing it?
Found it.
I didn't have the php filter module on which is required for this (and a lot of other useful things). Thanks so much.
Thanks for these comments. I
Thanks for these comments.
I am trying to convert a taxonomy term to a cck field via Rules. The term is "lost"
Event: Content is Going to be Saved
Condition: Execute Custom PHP Code
foreach ($node->taxonomy as $term) {if (strtolower($term->name) == 'lost') {
return true;
}
}
return false;
Action: Populate Saved Content's Field
Two things I do not understand.
1- Does it make a difference whether Event is "Content is going to be saved" or "After saving new content"
2- Does it matter at the Action: Populate stage whether content is "unchanged content" or "saved content"
Or is this simply not a good strategy? Thank you.
Covert a term to a cck field?
I'm not sure I understand what you are trying to do, are you trying to put the term into a cck field?
Thanks. This is about the
Thanks.
This is about the Mailhandler module. Mailhandler allows nodes to be created by sending email to a specific mailbox.
Mailhandler allows taxonomy terms to be assigned to incoming nodes, but not cck fields.
So, I thought I could assign a taxonomy to a node, which would then be used to populate a cck field via Rules.
I think I understand
Sounds like you are going about it the right way.
Both of your questions are related to 'use case.' There is more than one way to get things done, however sometimes for specific events you'll need to use one action/event or the other.
For example, I would use 'after content is saved' condition if I was using the action to populate a node-reference field (the content has to exist before I can refer to it).
If you don't need specific things like that for your example and either method will get the job done then use the one that makes the most sense for what you are trying to do. At least that is the way I understand it.
Thanks, it is exciting to
Thanks, it is exciting to learn about Mailhandler and do something tangible with Rules.
The Devel module seems to be stating the array is returning FALSE. This is exactly what I have entered:
foreach ($node->taxonomy as $term) {if (strtolower($term->name) == 'lost') {
return true;
}
}
return false;
I also tried Textual Comparison with [node:term] & lost (no quotes) and it worked. Unfortunately there are two taxonomy terms and it only returns the lowest value term. But at least I know it works.
Side note, per a comment above, to locate taxonomy term id's there is Taxonomy Manager module which displays terms and id's and allows for management of terms.
You might try...
I tried triggering off a taxonomy term similar to what you're trying to do. I did a bit of troubleshooting with the code that you got from caschbre in the third reply and found that it failed if I set the rule to evaluate when content was created or after it was saved, but it worked when content was viewed. It looks like the taxonomy object doesn't get loaded or isn't available when form is going to write to the db, but does load all of the properties (most importantly the 'name' property) when the content is read. I ended up using the vocabulary and term ids:
$return = false;$vid = 5;
$tid = 27;
if(isset($node->taxonomy)) {
if (array_key_exists($vid, $node->taxonomy) && $node->taxonomy[$tid] == $value) {
$return = true;
}
}
return $return;
It's not as elegant, but it does work.
Thanks. This might really
Thanks. This might really help people using Mailhandler.
As a side node, as to the event, I found that when I tried to convert a node it only worked when content was viewed, not when it was going to be saved or after being saved, and it did not actually need to be viewed, it was converted at a cron run.
Also, if anyone knows of a php snip for collecting and printing a password within Rules, I want to use Rules to send a new user email, customized by role.
So, it just occurred to me,
So, it just occurred to me, Rules can simply populate cck fields based on the node type created by Mailhandler, so there is no need to convert taxonomy... I think that would work for most cases.