Error message

You need to log in or create an account to access this page.
Advertising sustains the DA. Ads are hidden for members. Join today

Computed Field

Using Computed Fields inside and outside of Unlimited Value Multigroups

Last updated on
30 April 2025

Here is a working code example that contains computed fields in
unlimited type multigroups and a computed field outside of the
multigroup. It is for a work order system. The user enters
in hours and hourly rate for labor and they enter qty and unit price
for parts. The materials and labor costs all get totalled up:

I have a content type called Work Order that contains two
unlimited value multigroups, Labor and Parts.
The Labor multigroup contains four
fields:

  • Hours - Decimal - field_hours_work_order
  • Description - Text
  • Per Hour - Decimal - field_per_hour_work_order
  • Total - Computed - field_total_labor_work_order
    • Computed Code that multiplies hours and hourly rate
      • foreach (array_keys($node->field_hours_work_order) as $delta) {
          if (isset($node->field_hours_work_order[$delta]['value']) && isset($node->field_per_hour_work_order[$delta]['value'])) {
            $node_field[$delta]['value'] = $node->field_hours_work_order[$delta]['value'] * $node->field_per_hour_work_order[$delta]['value'];
          }
        };
    • Data Type
      • Decimal
    • Not Null

The Parts multigroup also contains four fields:

  • Qty - Decimal - field_quantity_parts_work_order
  • Description - Text
  • Unit Price - Decimal - field_unit_price_work_order
  • Total - Computed - field_total_parts_work_order
    • Computed Code that multiplies Qty and Price
      • foreach (array_keys($node->field_quantity_parts_work_order) as $delta) {
          if (isset($node->field_quantity_parts_work_order[$delta]['value']) && isset($node->field_unit_price_work_order[$delta]['value'])) {
            $node_field[$delta]['value'] = $node->field_quantity_parts_work_order[$delta]['value'] * $node->field_unit_price_work_order[$delta]['value'];
          }
        };
    • Data Type
      • Decimal
    • Not Null

Next, I have a field called Total
that adds up all the Labor and Parts totals:

  • Computed Code that adds up all Labor and Parts totals for a grand total
    • $laborpartstotal = 0;
      foreach (array_keys($node->field_total_labor_work_order) as $delta) {
        if (isset($node->field_total_labor_work_order[$delta]['value'])) {
          global $laborpartstotal;
          $laborpartstotal += $node->field_total_labor_work_order[$delta]['value'];
        }
      };
      
      foreach (array_keys($node->field_total_parts_work_order) as $delta) {
        if (isset($node->field_total_parts_work_order[$delta]['value'])) {
          global $laborpartstotal;
          $laborpartstotal += $node->field_total_parts_work_order[$delta]['value'];
        }
      };
      
      $node_field[0]['value'] = $laborpartstotal;
      
  • Data Type
    • Decimal
  • Not Null

As a final step, I load up the Manage Fields page of my Work Order in a
browser with javascript turned off so I can make sure the field weights
are ok as they determine the order which the calculations take
place. I use a Firefox plugin called QuickJava to switch
javascript off. Then I make sure that the computed fields have
higher number weights than the fields they reference or the
calculations will require multiple node saves to calculate everything
(Which is unacceptable for accounting stuff.)

Hope this helps someone. Let me know if I need to
clarify anything by sending me a PM here on drupal.org.

Peace, Jeff aka loopduplicate

Help improve this page

Page status: Not set

You can: