2

I have 2 taxonomy vocabularies (town and topic) which are referenced on both a user and a node. When a node is created, I need to email all users who are referencing any of the terms referenced on the node EXCEPT when the node has been directly assigned to a user (using a User Reference field).

The Subscriptions module works great for notifying users of all new nodes with a particular term, but I cannot find a way to exclude nodes based on other fields (in my case, the User Reference).

I can use Rules to send an email when a new node is created without the User Reference, but I don't know how to define the recipients as "all users with a referenced term which is also referenced on the new node". VBO may be the answer, but I'm lost on how to generate the View from Rules.

Any suggestions? Thanks.

7

Actually, you can pull up all users (or any entity) based on a field value by using the 'Fetch entity by property' action.

Since users (and nodes, and comments, etc) are all entities, you can use this action to pull up a list of users with a certain value in a field.

In your rule, that gets fired upon the update of a node, simply

  • add the 'Fetch entity by property' action,
  • select 'User' as the entity type, and
  • in the final screen choose the field (in your case a term reference) you want to check and the specific value you're checking for.
  • Feel free to change the name of the entities it will fetch for you. By default they will be called fetched_entity.

THEN, add a loop to your action set after this fetch, to go through each of the found entities. In this loop, we'll check for your second field and exclude the necessary ones. From what I understand, you'll want to exclude the ones whose uid matches the user reference field on the edited node.

So here's how:

By now your rule's action set should look like this: You're fetching users by your taxonomy property, then you'll looping through all the found entries (make sure it's pulling as many as you want, because it limits to 10 by default, by the way).

enter image description here

Now, add a component in your rules. Components are used when you need an entirely new rule to execute in your action area. Now that we're looping through users, we need an entirely new rule to call (IF the user doesn't have field x, THEN do y). However, when we're in the action area, we can't call conditions. So that's why we'll create a component that's a rule within itself, and call it from here.

  • Go to admin/config/workflow/rules/components or Configuration --> Workflow --> Rules --> Components
  • Click to "Add new component"
  • Select "Rule" for the component plug in. We don't want it to be just a condition test or an action, we want it to contain both a condition and action!
  • Call it something like "Email non-referenced users" or something like that
  • We will pass it two parameters. We need to pass it the user we're currently looping through and the node that has just been changed (that triggered the rule) because we'll compare the user's uid with the node's user reference field.

enter image description here

  • Add a condition to your component that is a 'Data comparison' condition and compare the looped_user:uid (or whatever you called that incoming parameter) to the user reference field of your updated_node (or whatever you called that incoming parameter)
  • Careful here, make sure to check the 'Negate' checkbox at the bottom of that condition. We want to let through only those users for whom the two fields DON'T match, i.e. the ones that aren't referenced.

enter image description here - As an action in the component, have the system send out the email you want

Voila! Almost done. This component now narrows down our looped users (with the correct terms) to those who aren't referenced and emails them. The one thing we have left to do is to CALL this component in our user loop.

Go back to your original rule, and click 'Add action' to your loop (not below it, but on it) enter image description here

In your list of actions, at the very, very bottom, you will see the names of components you have created. Your new component should be there. Just select it, and pass it the updated node and the 'fetched_entity' (which is your currently looped users) as the two parameters.

Test!

Hope this helps.

  • Wonderful! I don't actually need to exclude the referenced user, I need to skip the entire process if any user is referenced (in other words - send an alert only if this node is not assigned to anyone). This was easy to accomplish with a condition on the rule you suggested. It's working like a charm - thank you for explaining 'Fetch entity by property' and 'loops' so clearly! – Steven Dunwoody Aug 28 '13 at 18:47
  • Yay! I'm so glad it worked! – Boriana Ditcheva Aug 28 '13 at 18:51
  • Good answer. I had forgotten about Fetch Entity by Property. I shouldn't be so quick to go straight to code with Rules. – jazzdrive3 Aug 28 '13 at 19:07
0

You'll want to create you're own custom rules action in a custom module, using hook_rules_action_info(). http://drupalcontrib.org/api/drupal/contributions!rules!rules.api.php/function/hook_rules_action_info/7

This action should provide a list of email addresses. You can then loop over this list in your rule, sending a message to each one.

The action would look something like this:

function mymodule_rules_action_info() {
  return array(
    'mymodule_get_user_emails' => array(
      'label' => t('Get user emails'),
      'parameter' => array(
        'term' => array(
          'label' => t('Term'),
          'type' => 'taxonomy_term',
        ),
      ),
      'provides' => array(
        'emails' => array(
          'label' => t('List of Emails'),
          'type' => 'list<text>',
        )
      ),
      'group' => t('My Module'),
    ),
  );
}

function mymodule_get_user_emails($term) {
  // Do stuff here to get your list of emails based on the term.
  // $emails would be an array.
  return array('emails' => $emails);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.