Helo people,
i'm having problems doing something that should be so simple I will probably end up embarrassed with the solution, but anyway here it goes.
I have a content type with several CCK fields, one of which is a checkbox. I want to create a rule using the value of that checkbox to determine wether some other fields should be hidden.
I created a content type and enabled rules for it, then I created a new Triggered Rule using that form as the target (concretly using the "Registration node form is being built" event). Then I add a condition that targets the checkbox using the name I get from the registration form itself (by enabling "Display form element IDs" and going to the form).
Now, no matter what I put on the "Form Element ID" (tried with and without [value], appending [0], mixing both...) or the "value(s)" (tried with on, 1, yes, etc) field, I never get a hit for the rule.
Any hints on how to use a checkbox for the condition of a rule?
Thanks in advance!
Comments
Instead of Rules you might
Instead of Rules you might consider using Conditional Fields module.
Yeah.
Conditional Fields is the module you want.
Now: Off to do some approving of Swedish translations for Drupal seven!
//Johan Falk
Thanks guys, but..
...I need some other functionalities from the rules module (I want to put some other actions besides what I described) , so I'm still interested in knowing how to get that checkbox value in my condition, hints?
So, you want the form to
So,
you want the form to change based on the checkbox being checked after the form is already built? This would require jquery or ajax.
Or are you saying the checkbox has a value property associated with it and you are trying to read that property? need some more specifics about your checkbox before a meaningful answer can be given.
No
I dont want the form to change using jscript, this evaluation should happen before the form is built (im doing it in the "Registration node form is being built" event).
use case is:
user creates node with registration information
once saved, he gets a PDF with instructions to do a bank transfer
when he does the transfer, he comes back and marks a checkbox indicating it
after that, and with the data already in the db, if the user comes back to edit that node, he can't because a rule changes it
that last step is what i'm trying to accomplish, if you have an alternative its welcome! (maybe another module)
I guess thid msy also be done in a module using form_hook, but using rules, I already have everything else and I only need a way to get the checkbox value correctly.
thanks!
Well
This may end up as a bug, I've debugged the problem manually by introducing "drupal_set_message" lines in the file rules_forms/rules_forms.rules.inc and that has lead me to the function "rules_forms_condition_element_value" (line 334) which in my case leads to calling "rules_forms_equal_array_values" through line 347 (just so you know the exact branching). In that function I put some debug messages to get the contents of the arrays to be compared and they give me this:
Array1: Array ( [0] => on ) Array2: Array ( [0] => Array ( [value] => on ) )I gues you see the problem, but I don't have a solution yet.
What do you think, is this a bug?
Thanks!
workflow module might be a
workflow module might be a way to accomplish this - you could define a separate workflow state for whether or not your user action has happened - and once it has - put the node in a state where only certain role can edit it.
Y0u probably should start by just setting a rule that when the form you are trying to work with is being built - set a system message and have the body of the message be the $form_state variable. This would then give you the exact form element. When you actually edit a node where it has the checkbox checked - you should see that value in your $form_state.
Thanks for the idea, but
Thanks for the idea, but after trying it I realize that if I follow that path I will need to redefine many things to fit the workflow module with what I have and I don't have time for that.
So, I've patched my rules module (to be clear, I hate to do that) so it works as I need. The changes I've done are:
-Add a new function, taken directly from php.net, that compares multidimensional arrays (it goes deeper in the array values if they are arrays):
function multidimensional_array_diff($a1,$a2)
{
$r = array();
foreach ($a2 as $key => $second)
{
foreach ($a1 as $key => $first)
{
if (isset($a2[$key]))
{
foreach ($first as $first_value)
{
foreach ($second as $second_value)
{
if ($first_value == $second_value)
{
$true = true;
break;
}
}
if (!isset($true))
{
$r[$key][] = $first_value;
}
unset($true);
}
}
else
{
$r[$key] = $first;
}
}
}
return $r;
}
-secondly I changed the function rules_forms_equal_array_values to use this comparation function:
function rules_forms_equal_array_values($array1, $array2) {$diff1 = multidimensional_array_diff($array1, $array2);
$diff2 = multidimensional_array_diff($array2, $array1);
return empty($diff1) && empty($diff2);
}
-And lastly I added some line to the rules_forms_condition_element_value function so it converts the offending array to another one that is compatible with the array coming from $form_value (as I explained previously). So my rules_forms_condition_element_value function looks like (what I added is in bold):
function rules_forms_condition_element_value($form, $form_state, $element, $value) { $form_element = _rules_forms_get_element($form, $element); if (isset($form_element['#value'])) { // Multiple values come in as array if (is_array($form_element['#value'])) { $lines = explode("\r\n", $value); return rules_forms_equal_array_values($lines, $form_element['#value']); } return $form_element['#value'] === $value; } if (isset($form_element['#default_value'])) { if (is_array($form_element['#default_value'])) { $lines = explode("\r\n", $value); $lines2 = array(); foreach($lines as $line) { $lines2[] = array('value'=>$line); } $lines = $lines2; return rules_forms_equal_array_values($lines, $form_element['#default_value']); } return $form_element['#default_value'] === $value; } return FALSE; }Now, of course, this is a REALLY DIRTY SOLUTION™, so if anybody comes up with a Clean and Elegant Solution™ it's more than welcome ;).
For now the fact is it works for me, and that's enough for now as I need to have this system working before the year ends, but I still consider all this a bug.
Thank you for reading this far!
Update1: Changed a <code> to <pre> so the <strong> tag works