2

I have a number of values that I need to run a bit of logic on to get a value for a views math expression further down the chain. Im going to provide a much simpler example of what I am actually doing...

Variables set on custom module admin page...

  • twoyr_a = 5.4
  • twoyr_b = 12.4
  • threeyr_a = 5.6
  • threeyr_b = 12.7
  • fouryr_a = 5.8
  • fouryr_b = 13.3

Contract term (Factor A) - Overidden with views-views-field--factor--a.tpl.php

if ($output == '24') {
    print variable_get('twoyr_a');
} elseif ($output == '36') {
    print variable_get('threeyr_a');
} elseif ($output == '48') {
    print variable_get('fouryr_a');
} else {
    print '0';
}

Contract term (Factor B) - Overidden with views-views-field--factor--b.tpl.php

if ($output == '24') {
    print variable_get('twoyr_b');
} elseif ($output == '36') {
    print variable_get('threeyr_b');
} elseif ($output == '48') {
    print variable_get('fouryr_b');
} else {
    print '0';
}

Views Global:Math Expression

[factor_a] + [factor_b] = result

However, the result is always wrong. Instead of using the result from the PHP template, it is adding the Contract terms (24 + 24 = 48).

Im guessing this is because the math expression runs before the template files do.

So my question is, is there anyway to avoid using Views PHP for this? Or is there a fix for my current solution?

Note: $output is already defined in the database as 24, 36, or 48 in a "Contract Term" column. It relates to just a normal field in an ECK entity type called "Deal". It's an integer value to define the length of a contract for that deal. Each contract term has its own A and B factors to calculate my clients profit percentage when borrowing from a loan company.

  • Note: I considered using computed field to run the logic and store a value in the database, but as I have 10 thousand rows, this will add another 20 thousand rows to the database. I guess another question is whats going to be worse for performace, views php, or 20k more database rows! – Collins May 27 '16 at 10:02
  • Hi Pierre, $output is already defined in the database as 24, 36, or 48 in a "Contract Term" column. – Collins May 27 '16 at 10:19
  • Its just a normal field in an eck entity type called "Deal". Its an integer value to define the length of a contract for that deal. Each contract term has its own A and B factors to calculate my clients profit percentage when borrowing from a loan company – Collins May 27 '16 at 10:24
  • I am going to change the way I am doing this and use a computed field instead as I will need to expose this value to search in views. – Collins May 27 '16 at 10:30
3

A possible solution that might help to simplify the challenge in your question, is to use the fairly new Math Field module (for D7). Some more details about this module (from its project page):

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.

By creating some auxiliary field(s) (calculated via the Math field), you may be able to complete your puzzle. Possibly via some additional logic implemented via the Rules module, to implement logic that is similar to what I described in my answer to "How to set the values of some fields depending on the value of a list box?".

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

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.