2

I've created several webforms. 5 of them contain a field with same machine-name where 2 different rules check the value of the mentioned field. Depending on that value an e-mail is sent. My problem is that both rules run on all webforms, not just on the 5 I only want them to. As the field it tries to check is not present in the other webforms, I get an error message on all other webforms. How can I limit the rule to run on only these 5 of my webforms?

PS: In my rules under "Condition" I execute an PHP code to check the value.

I guess it could be solved with AND or OR but couldn't figure it out.

1

Typical approach

The previous answer quite often resolves various situations where some variables appear to not be available in Rules Actions, etc. For example, before using a Rules Action like "Set a data value" for your a field, you have to make sure to add a Rules condition Entity has field (related to the field for which you want to set a value). And make sure to add that Entity has field condition BEFORE other Rules Conditions in which you might want to refer to this field.

That's also what is mentioned in the Rules UI, e.g. when you're adding a "data comparison" condition: somewhere it says "The data selector helps you drill down into the data available to Rules. To make entity fields appear in the data selector, you may have to use the condition 'entity has field' (or 'content is of type')".

For a video tutorial that illustrates the importance of this Entity has field condition, refer to Data types and data selection, especially what is shown between about 13:30 and 17:30 in it.

Webform requires another approach

However, for this specific question, which is about using Rules in combination with Webform, things are a very little bit more complicated, as further explained below.

Let's assume I have a webform created with some of its fields with machine names like so:

  • contact_us_first_name.
  • contact_us_last_name.
  • contact_us_subject.

After you add these fields (with identical machine names) to any of your own forms, you should be able to import the prototype-rule below (using the Rules UI) in your own environment (without running into an integrity constraint):

{ "rules_create_a_node_from_a_submitted_webform" : {
    "LABEL" : "Create a node from a submitted webform",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "webform_rules" ],
    "ON" : { "webform_rules_submit" : [] },
    "IF" : [
      { "user_has_role" : { "account" : [ "user" ], "roles" : { "value" : { "2" : "2" } } } }
    ],
    "DO" : [
      { "drupal_message" : { "message" : "The webform component submitted by [data:contact_us_first_name-value] (= [data:contact_us_first_name-title]) [data:contact_us_last_name-value] (= [data:contact_us_last_name-title]) contains a field labeled \u0022[data:contact_us_subject-title]\u0022 which has value \u0022[data:contact_us_subject-value]\u0022." } }
    ]
  }
}

In the above rule I included an extra Rules Condition to prevent the Rules Action from being executed for anonymous users (feel free to adapt the role id of it, or just remove that Rules Condition). And the rule is triggered via Rules Event "After a webform has been submitted".

The Rules Action only displays an (informational) message on the site (my recommended way of writing and debugging Rules ...) after a webform gets submitted. Here is what I specified (in the Rules UI) as the message to be shown:

The webform component submitted by [data:contact_us_first_name-value] (= [data:contact_us_first_name-title]) [data:contact_us_last_name-value] (= [data:contact_us_last_name-title]) contains a field labeled "[data:contact_us_subject-title]" which has value "[data:contact_us_subject-value]".

Here is a sample of the message shown in my own setup:

The webform component submitted by Dries (= First name:) Buytaert (= Last name:) contains a field labeled "Subject:" which has value " Rumour goes that the Rules module for D8 is making progress! ".

What the above prototype-rule shows is that the "clue" to answer your question is that you have access to all labels and all values of any field of a webform available, ie:

  • [data:MachineFieldName-title] contains the Label of a field with machine name MachineFieldName.
  • [data:MachineFieldName-value] contains the Value of a field with machine name MachineFieldName

All that's left to fully answer your question now is to correct your Rule using similar tokens.

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.