1

Use Case:

I've got two select lists

  1. Due Date
  2. Status

The Due Date is a Date field, the Status is a select list with options: Current, Past Due.

By default, Current is pre-selected.

I want to run a Rule that when the current date is greater than the Due Date, the value of the select list will change to Past Due.

I was trying to experiment with the Rules module, not sure about the event. Would it be Drupal is initializing?

What's the best approach?

0
  1. Add event entity has a value or data comparison
  2. Add condition according to your select list
  3. Add action set data value against changed value
0

I'm not sure if by using Rules Event "Drupal is initializing" you could get it to work. But I recommend you to try to use an alternative Rules Event, since a Rule that uses Rules Event Drupal is initializing gets triggered on every (repeat every) page load on your site. There are plenty of situations where this is (probably) the only Rules Event you can use (if you want to implement some logic with Rules), but I always try to use that Rules Event only if anything else fails.

In your case, there is a much better suited Rules Event you could use, i.e. Rules Event Before saving content. Have a look at this rule (in Rules Export format):

{ "rules_force_a_value_for_past_due_dates" : {
    "LABEL" : "Force a value for past due dates",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "node_presave" : [] },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_expiration_date" } },
      { "data_is" : {
          "data" : [ "node:field-expiration-date" ],
          "op" : "\u003C",
          "value" : [ "site:current-date" ]
        }
      },
      { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_optionlist" } }
    ],
    "DO" : [
      { "data_set" : { "data" : [ "node:field-optionlist" ], "value" : "A" } }
    ]
  }
}

Some more details to further explain the above above sample:

  • The Rules Event is "Before saving new content" (if appropriate, you could limit it to some content type, like "Article", only, if the Rule should only be triggered for such content type).
  • In my case I have a selection list field with machine name field_optionlist.
  • This field actually has 3 possible values, like so:
    • A (=Advanced).
    • B (=Basic).
    • C (=Custom).
  • These are its Rules Conditions (validations), in the order specified:
    • a validation to ensure that this rule only applies for nodes that have a field with machine name field_expiration_date (which also makes that field available as a token for processing in subsequent Rules Conditions/Actions).
    • a validation about attempts to save a node where "The selected value" of field_expiration_date is in the past.
    • a validation to ensure that this rule only applies for nodes that have a field with machine name field_optionlist.
  • If all these Rules Conditions are satisfied, then the Rules Action will force the value of field_optionlist to be A (feel free to change to whatever you want).

If you want, you can use the Rules UI to import the above rule in your own site. But before doing so, make sure you have defined 2 similar fields (with machine names field_optionlist and field_expiration_date). As a variation, adapt the machine names to similar fields you already have (attention about field-optionlist versus field_optionlist though, same for the 2nd field, important!).

Easy, no?

PS 1: If you're not familiar (enough/yet) with Rules, checkout the 32 (!!!) great, and free, video tutorials Learn the Rules framework.

PS 2: I'm assuming this question is about D7.

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.