TransWikia.com

Using computed field value in Rules

Drupal Answers Asked on January 4, 2022

I’m using Commerce module and I have a line item with a computed field that’s calculating a date. This field finally shows a date.

Then during the checkout process I have a form with a Date field (part of the Order). I’m running some Rules and finally I have to copy one of the calculated dates to this field but i couldn’t by rules.

I tried with a simple rule "set a data value" trying to copy the Computed field value to the other field. I put a "has field" with the CF. Then in actions, the field doesn’t appear in the data selector.

This is normal? Is there another method to do it?

2 Answers

Anyone running into this recently, I found a work around.

I used "Entity has field" as a condition on the Computed Field Value.

I then used the "Add a variable" action to add a text value of the Computed Field as a variable.

Then added a "Convert Data Type" action to turn the value into an integer.

I could then use "Set a data value" as normal.

Here is an export of my rule (for a different use case), but still setting Computed Field values as a Variable...

{ "rules_set_default_remaining_balance" : {
    "LABEL" : "Set Default Remaining Balance",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "entityform" ],
    "ON" : { "entityform_insert" : [] },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "entityform" ], "field" : "field_total_price" } },
      { "entity_has_field" : { "entity" : [ "entityform" ], "field" : "field_remaining_balance" } },
      { "data_is_empty" : { "data" : [ "entityform:field-remaining-balance" ] } }
    ],
    "DO" : [
      { "variable_add" : {
          "USING" : { "type" : "text", "value" : [ "entityform:field-total-price" ] },
          "PROVIDE" : { "variable_added" : { "variable_total_price" : "Total Price" } }
        }
      },
      { "data_convert" : {
          "USING" : { "type" : "integer", "value" : [ "variable-total-price" ] },
          "PROVIDE" : { "conversion_result" : { "conversion_result" : "Conversion result" } }
        }
      },
      { "data_set" : {
          "data" : [ "entityform:field-remaining-balance" ],
          "value" : [ "conversion-result" ]
        }
      }
    ]
  }
}

In the example above, the field "Total Balance" is being Computed.

Answered by Collins on January 4, 2022

You may want to give it a try to use the NOT commonly known/used alternative available for the Computed Field module, i.e. the (fairly new) Math Field module (for D7). Some more details about this module (from its project page), which also details when the calculations actually happen:

The Math Field module defines a new field type to dynamically calculate values on entity forms. This is an alternative to Computed Field that does not require the use of PHP Filter.

Math expression fields are dynamically updated via ajax on the entity form as soon as all necessary fields are populated. If JavaScript is disabled, the field will be evaluated when the form is submitted. The result is stored in the database when the form is submitted.

For more details (step-by-step instructions), refer to either of these links:

Known issues

As this is a fairly new D7 module, there are still a few (known) issues with it, as mentioned also on its project page:

Bonus: you would not need the "PHP filter" (IMO only that should be a valid reason to investigate this alternative) ...

Answered by Pierre.Vriens on January 4, 2022

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP