You are here

function farm_quantity_log_update_measurement_by_label in farmOS 7

Helper function for updating the value of a quantity measurement on a log, based on its label.

Parameters

\Log $log: A log entity.

string $label: The label of the quantity measurement that needs updating.

string|float $value: The new quantity measurement value.

File

modules/farm/farm_quantity/farm_quantity_log/farm_quantity_log.module, line 464
Farm quantity log module.

Code

function farm_quantity_log_update_measurement_by_label($log, $label, $value) {

  // Load the log entity metadata wrapper.
  $log_wrapper = entity_metadata_wrapper('log', $log);

  // If there are no quantities, bail.
  if (empty($log_wrapper->field_farm_quantity)) {
    return;
  }

  // Iterate over the quantities.
  foreach ($log_wrapper->field_farm_quantity as $quantity) {

    // If a label matches, update it.
    if ($quantity->field_farm_quantity_label
      ->value() == $label) {

      // Clear the old value.
      $quantity->field_farm_quantity_value = array();

      // Add the new value.
      $value_fraction = fraction_from_decimal($value);
      $quantity->field_farm_quantity_value->numerator
        ->set($value_fraction
        ->getNumerator());
      $quantity->field_farm_quantity_value->denominator
        ->set($value_fraction
        ->getDenominator());

      // Save the quantity.
      $quantity
        ->save();
    }
  }
}