0

I'm struggling with adding $ prefix to fields within nested, multi-value field collections. I'm not sure how to build this loop. I've tried to add each field to an array then using foreach to add '#prefix' = '$'. It's not quite working. When I add additional field collections, only the last instance gets the prefix: enter image description here

Any ideas?

$dollar_fields = array(
        &$form['field_estimated_value'], 
        &$form['field_down_payment'],
        &$form['field_amount_new'],
        &$form['field_appraisal_cost'],
        &$form['field_credit_report'],
        &$form['field_lender_s_inspection'],
        &$form['field_processing_fee'],
        &$form['field_underwriting_fee'],
        &$form['field_wire_transfer'],
        &$form['field_closing_costs'],
        &$form['field_lender_finders_fee'],
        &$form['field_property_tax_holdback'],
        &$form['field_lien_hold_back'],
        &$form['field_bonus_discount_paid_by_bor'],
        &$form['field_brokerage_fee'],
        &$form['field_evaluation_fee'],
        &$form['field_inspection_costs'],
        &$form['field_insurance_fees'],
        &$form['field_participant_tax_amount'],
        &$form['field_agent_commission'],
        &$form['field_appraisal_amount'],
        &$form['field_total_asset_dp'],
        &$form['field_total_prior_to_advance'],
        &$form['field_total_from_proceeds'],
        &$form['field_total_payments_remaining'],
        &$form['field_capped_payments'],
        &$form['field_legal_fees_disbursments'],
        &$form['field_total_closing_costs'],
        &$form['field_1st_mortgage'],
        &$form['field_2nd_mortgage'],
        &$form['field_3rd_mortgage'],
        &$form['field_assets_value'],
        &$form['field_liabilities_limit'],
        &$form['field_liabilities_balance'],
        &$form['field_liabilities_payments'],
        &$form['field_total_assets'],
        &$form['field_total_liabilities'],
        &$form['field_net_worth'],
    );

    foreach ($form['field_applicant_property']['und'] as $delta => $field) {
        if (is_numeric($delta)){
            $app_property = array(
                &$form['field_applicant_property']['und'][$delta]['field_property_value'],
                &$form['field_applicant_property']['und'][$delta]['field_original_value'],
                &$form['field_applicant_property']['und'][$delta]['field_annual_taxes'],
                &$form['field_applicant_property']['und'][$delta]['field_annual_taxes_m'],
                &$form['field_applicant_property']['und'][$delta]['field_condo_fees_m'],
                &$form['field_applicant_property']['und'][$delta]['field_heating_m'],
                &$form['field_applicant_property']['und'][$delta]['field_rental_income_m'],
                &$form['field_applicant_property']['und'][$delta]['field_hydro_m'],
                &$form['field_applicant_property']['und'][$delta]['field_management_m'],
                &$form['field_applicant_property']['und'][$delta]['field_repairs_m'],
                &$form['field_applicant_property']['und'][$delta]['field_other_m'],
                &$form['field_applicant_property']['und'][$delta]['field_insurance_m'],
                &$form['field_applicant_property']['und'][$delta]['field_interest_m'],
                &$form['field_applicant_property']['und'][$delta]['field_total_monthly_expenses'],
            );

        foreach ($form['field_applicant_property']['und'][$delta]['field_property_mortgage']['und'] as $delta2 => $field2) {
            if (is_numeric($delta2)){
                $mort_field = &$form['field_applicant_property']['und'][$delta]['field_property_mortgage']['und'][$delta2];
                $app_mortgage = array(
                    $mort_field['field_mortgage_balance'],
                    $mort_field['field_payments'],
                    $mort_field['field_early_discharge_penality'],
                );
            }
        }
    }
    }

    foreach ($form['field_employment']['und'] as $delta => $field) {
        if (is_numeric($delta)){

            $emp_field = &$form['field_employment']['und'][$delta];
            $app_employment = array(
                &$emp_field['field_gross_revenue'],
                &$emp_field['field_income_amount'],
                &$emp_field['field_self_declared_income'],
            );
        }
    }

    foreach ($form['field_assets']['und'] as $delta => $field) {
        if (is_numeric($delta)){

            $ass_field = &$form['field_assets']['und'][$delta];
            $app_assets = array(
                &$ass_field['field_asset_value'],
                &$ass_field['field_asset_down_payment'],
            );
        }
    }

    foreach ($form['field_mortgage']['und'] as $delta => $field) {
        if (is_numeric($delta)){

            $mort_field = &$form['field_mortgage']['und'][$delta];
            $app_new_mortgage = array(
                &$mort_field['field_annual_taxes'],
                &$mort_field['field_condo_fees_m'],
                &$mort_field['field_hydro_m'],
                &$mort_field['field_management_m'],
                &$mort_field['field_interest_m'],
                &$mort_field['field_total_monthly_expenses'],
                &$mort_field['field_insurance_m'],
                &$mort_field['field_repairs_m'],
                &$mort_field['field_other_m'],
                &$mort_field['field_purchase_price'],
                &$mort_field['field_estimated_value'],
                &$mort_field['field_appraisal_amount'],
                &$mort_field['field_recommended_value'],
                &$mort_field['field_heating_m'],
                &$mort_field['field_mortgaeg_amount_new'],
                &$mort_field['field_total_loan_amount'],
                &$mort_field['field_monthly_payment'],
                &$mort_field['field_property_tax_in_arrears'],
                &$mort_field['field_dp_total'],
                &$mort_field['field_improvement_value'],
                &$mort_field['field_property_value_new'],
                &$mort_field['field_down_payment_new'],
                &$mort_field['field_1st_mortgage'],
                &$mort_field['field_2nd_mortgage'],
                &$mort_field['field_3rd_mortgage'],
            );
        }
    }

    foreach ($form['field_liabilities']['und'] as $delta => $field) {
        if (is_numeric($delta)){

            $liability_field = &$form['field_liabilities']['und'][$delta];
            $app_liabilities = array(
                &$liability_field['field_liability_limit'],
                &$liability_field['field_liability_balance'],
                &$liability_field['field_liability_payment'],
            );
        }
    }


    foreach ($form['field_investor_s']['und'] as $delta => $field) {
        if (is_numeric($delta)){

            $invest_field = &$form['field_investor_s']['und'][$delta];
            $app_investors = array(
                &$invest_field['field_investment_amt'],
            );
        }
    }


    $percentage_fields = array(
    &$form['field_mortgage']['und'][0]['field_ltv'],
    &$form['field_mortgage']['und'][0]['field_interest_rate'],
    &$form['field_mortgage']['und'][0]['field_secondary_rate'],
        &$form['field_mortgage']['und'][0]['field_buy_down_rate'],
        &$form['field_mortgage']['und'][0]['field_net_rate'],
        &$form['field_mortgage']['und'][0]['field_variable_to_prime'],
        &$form['field_mortgage']['und'][0]['field_escalation_rate'],
   );
    $all_dollars = array_merge($dollar_fields, $app_property, $app_mortgage, $app_employment, $app_assets, $app_liabilities, $app_new_mortgage, $app_investors);

    foreach ($all_dollars as &$field) {
        //dpm($field);
        $field['und'][0]['value']['#field_prefix'] = '$';   
    }

    foreach ($percentage_fields as &$field) {

        $field['und'][0]['value']['#field_suffix'] = '%';   
    }   
1
  • Show us your code. Commented Sep 18, 2015 at 0:52

1 Answer 1

0

The reason for prefix not setting is,

Firstly, you are setting the values in the foreach loop which is not affecting the actual $form array.

Your code:

foreach ($all_dollars as &$field) {
  $field['und'][0]['value']['#field_prefix'] = '$';   
}

It should have been: $form[field_name] instead of $field.

Secondly, field_prefix can be applied to the form field not to the value of the form field.

Finally, array_merge is not going to help here. You have to apply the value directly to the $form.

$form['field_mortgage']['#field_prefix'] = '$';
1
  • Thank you. For the last part, it's being applied after the value because it's a nested Field Collection, so value is actually a field. The code I have works but as you can see from the screenshot, not on all instances of the field collection. Commented Sep 18, 2015 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.