Rules based on multivalue cck fields

Events happening in the community are now at Drupal community events on www.drupal.org.
gomanual's picture

My goal is to give users the option of sending out notifications to other users of specified roles when they create a certain type of node. My current (not working) approach was to create a CCK text field configured to show check boxes (there are four options) on the node creation page, then use rules to catch the check boxes and send emails.

The CCK text field is called 'field_notify', with allowed values of 'group 1', 'group 2', 'group 3', and 'group 4'.

Rule set up:
ON After Saving New Content
IF Created content is Member Post
AND Saved content's field 'field_notify' has value <--- this is the problem
DO Send a mail to all users of a role.

I can't seem to get the second condition to evaluate properly regardless of what I put in the little code box. I tried PHP's string comparison operators, I tried using preg_match, I even tried using the == operator. No luck. On a whim I even tried switching the field to a number field; no dice.

Any ideas?

Comments

Success!

gomanual's picture

I changed the condition to "Execute php code" and entered:

$teststr = '/group 1/';
$return = false;
for($i=0; $i<=3; $i++) {
   if (isset($node->field_notify[$i]['value']) && preg_match($teststr,$node->field_notify[$i]['value']) != 0) {
     $return = true;
    }
}
return $return;

Which means I still ended up with four rules (one for each check box). That's okay; it works! Hopefully this will be useful to someone else.

Extended solution

tekken's picture

This code for "Execute php code" returns true for all nodes with the cck autotagging field "field_tags" that include one of the sets of tags.

# sets of tags to match
$ttms[0]="tag1";
$ttms[1]="tag1,tag3,tag4";
$ttms[2]="tag2,tag5";

function match_sets_to_cck_field($ttm, $node){
  $ttm_array=explode(",", $ttm);
  $ttmc=count($ttm_array);
  $terms_array=array();
  foreach ($ttm_array as $ttm) {
    $terms=taxonomy_get_term_by_name($ttm);
    $terms_array=array_merge($terms_array, $terms);
  }
  foreach ($terms_array as $term) {
    foreach ($node->field_tags as $field_tags){
      if ($term->tid == $field_tags['value']){$mc++;}}
  }
  if ($mc == $ttmc){return true;}
}

foreach($ttms as $ttm){
  if (match_sets_to_cck_field($ttm, $node)){return true;}
}

return false;

Populate all checkbox values of a Group

neorg's picture

I'am using rules to copy content to a other node type. Both node types has the same cck checkboxes. If you want to populate all values of a CCK group checkboxes is very simple by placing someting like this in Advanced: Specify the fields value with PHP code

return $node->field_CCK_FIELDNAME;

(where you replace CCK_FIELDNAME with the fieldname you used while making the cck field.)

Rules

Group organizers

Group categories

Categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: