AnswerBun.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!

Related Questions

” a field on Manage display

1  Asked on February 6, 2021 by carrotandme

   

Display Entity Reference field twice, with different display modes

1  Asked on February 3, 2021 by paul-canning

     

Media html not rendering in blocks

1  Asked on February 3, 2021 by kristoffer-rom

   

How can I programmatically change the view mode?

4  Asked on February 3, 2021 by jmzea

   

How do I avoid API calls are cached?

2  Asked on February 2, 2021 by user1648228

 

Custom JSON api normalizer is not initiated

0  Asked on February 1, 2021 by kevinvhengst

   

Suddenly unable to add a field to a new content type

1  Asked on January 31, 2021 by leif

     

an error when changing the date form

1  Asked on January 30, 2021 by mahdi-alikhasi

 

Display content based on taxonomy with view

2  Asked on January 30, 2021

 

Why is my code called again written on build form?

0  Asked on January 29, 2021 by namit-garg

     

How to change path of taxonomy facet

1  Asked on January 24, 2021 by rahulkumar-it

   

Form field not show on Ajax Callback even the callback is called

1  Asked on January 23, 2021 by emad-karhely

     

Features to import content types

2  Asked on January 18, 2021 by webmaster-pf

   

Redirect user to previous page after registration

2  Asked on January 15, 2021 by lbogdan

     

field_info_field: how to define the content type name?

1  Asked on January 14, 2021 by bruno-von-paris

   

Ask a Question

Get help from others!

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