Score:0

"Object of class Drupal\Core\Render\Markup could not be converted to number"

in flag

I am receiving an error on a webform I'm developing. Just to give some insight into the webform, the webform is taking in some data (customer type and monthly water consumption) and calculating the cost for Water, Solid Waste, and Sewer. I'm using computed twigs to calculate those cost, and I'm displaying them on the form. (This is where things get complicated). I am displaying current costs and new costs because we'll be applying a new percentage rate. So the user will be able to see current and new costs for Water, Solid Waste, and Sewer. All of this is working.

Now where I'm getting the error is calculating Final Current Cost and Final New Cost. (Basically, the Final Bill, adding Water, Solid Waste, and Sewer to get the Final Bill). I'm using a computed twig for Final Current Bill and Final New Bill.

I am just trying to add

{{ data.current_water_bill + data.current_wastewater_bill + data.current_solid_waste_bill }} 

to get Final Current Bill.

But I'm receiving this error:

Notice: Object of class Drupal\Core\Render\Markup could not be converted to number in __TwigTemplate_b425658712bf48861094edd9faa7ac1869e3493432f58af037adf7477e731085->doDisplay() (line 39 of /tmp/rolling/26f07ce/twig/6255afcf3e700___string_template__3115aa_bPBQWZ4Ws1n7j52GBOzrbmeqE/rL677g5Ips8i5OfG_zSuLsRcL3s-sFwxT2_H3MJmsfo.php).

Provide details and any research I've dedicated some time into the error looking for the same error online. I've found a mixture of issues, but ultimately it looks like I need to convert the data into a number. I tried doing so using a twig filter |number_format and then I get a new error:

Notice: Object of class Drupal\Core\Render\Markup could not be converted to float in twig_number_format_filter() (line 611 of /code/vendor/twig/twig/src/Extension/CoreExtension.php).

I can calculate and display the values but I just can't add them. I've added the data into variables and still can't work with them in twig. Any ideas or suggestions would be appreciated.

row:
'#type': flexbox
customer_type:
  '#type': select
  '#title': 'Customer Type'
  '#options':
    Industrial: Industrial
    Landscape: Landscape
    Multifamily: Multifamily
    Non-Residential: Non-Residential
    Reclaimed: Reclaimed
    Residential: Residential
  '#required': true
row_3:
'#type': flexbox
'#states':
  visible:
    ':input[name="customer_type"]':
      filled: true
monthly_consumption:
  '#type': number
  '#title': 'Monthly Consumption (in thousands of gallons)'
  '#required': true
  '#max': 100
  '#step': 1
row_6:
'#type': flexbox
'#states':
  visible:
    ':input[name="customer_type"]':
      value: Multifamily
number_of_units:
  '#type': number
  '#title': 'Number of Units'
  '#required': true
row_9:
'#type': flexbox
'#states':
  visible:
    ':input[name="customer_type"]':
      value: Residential
alley_customer:
  '#type': select
  '#title': 'Are you an alley customer? '
  '#options':
    'Yes': 'Yes'
    'No': 'No'
  '#required': true
row_2:
'#type': flexbox
'#states':
  visible:
    ':input[name="customer_type"]':
      filled: true
water_container:
  '#type': fieldset
  '#title': Water
  row_4:
    '#type': flexbox
    current_water_bill:
      '#type': computed_twig
      '#title': 'Current Water Bill'
      '#template': |
        <div class="form-text required form-control">
            {# Global Variables
            ================================================== #}
            {% set customer = data.customer_type %}
            {% set monthlyConsumption = data.monthly_consumption %}

            {# If/Else Statement Based on Customer Type #}
            {% if customer == 'Residential' %}
                {# Residential Monthly Water Volume Charges
                ========================= #}
                {% set baseFee = 10.35 %} {# using 3/4 inch Meter Size as Default Price for Residential houses #}
                {% set firstTenKVol = 1.60 %}
                {% set nextTenKVol = 2.08 %}
                {% set nextFourtyKVol = 2.62 %}
                {% set overSixtyKVol = 3.27 %}

                {# Residential Math Calculation
                ========================= #}
                {% set firstTenK = (monthlyConsumption * firstTenKVol) + baseFee|number_format(2) %}
                {% set nextTenK = ((monthlyConsumption - 10) * nextTenKVol) + (10 * firstTenKVol) + baseFee|number_format(2) %}
                {% set nextFourtyK = ((monthlyConsumption - 20) * nextFourtyKVol) + (10 * nextTenKVol) + (10 * firstTenKVol) + baseFee|number_format(2) %}
                {% set overSixtyK = ((monthlyConsumption - 60) * overSixtyKVol) + (40 * nextFourtyKVol) + (10 * nextTenKVol) + (10 * firstTenKVol) + baseFee|number_format(2) %}

                {# If/Else Statement Based on Residential Water Monthly Consumption, 
                    Will Display Math Calculations as Current Water Bill
                ========================= #}
                {% if monthlyConsumption >= 61 %}
                    <p>${{ overSixtyK }}</p>
                {% elseif monthlyConsumption <= 60 and monthlyConsumption >= 21 %}
                    <p>${{ nextFourtyK }}</p>
                    {% elseif monthlyConsumption <= 20 and monthlyConsumption >= 11 %}
                    <p>${{ nextTenK }}</p>            
                {% elseif monthlyConsumption <= 10 %}
                    <p>${{ firstTenK }}</p>
                {% endif %}
            
            {% elseif customer == 'Multifamily' %}
                {# Multifamily Monthly Water Volume Charges
                ========================= #}
                {% set baseFee = 29.31 %} {# using 2 inch Meter Size as Default Price for Multifamily houses #}
                {% set firstTenKVol = 0.87 %}
                {% set nextTenKVol = 1.07 %}
                {% set nextTwentyKVol = 1.47 %}
                {% set overFourtyKVol = 2.21 %}

                {# Multifamily Math Calculation
                ========================= #}
                {% set firstTenK = (monthlyConsumption * firstTenKVol) + baseFee|number_format(2) %}
                {% set nextTenK = ((monthlyConsumption - 10) * nextTenKVol) + (10 * firstTenKVol) + baseFee|number_format(2) %}
                {% set nextTwentyK = ((monthlyConsumption - 20) * nextTwentyKVol) + (10 * nextTenKVol) + (10 * firstTenKVol) + baseFee|number_format(2) %}
                {% set overFourtyK = ((monthlyConsumption - 40) * overFourtyKVol) + (20 * nextTwentyKVol) + (10 * nextTenKVol) + (10 * firstTenKVol) + baseFee|number_format(2) %}

                {# If/Else Statement Based on Multifamily Water Monthly Consumption
                ========================= #}
                {% if monthlyConsumption >= 41 %}
                    <p>${{ overFourtyK }}</p>
                {% elseif monthlyConsumption <= 40 and monthlyConsumption >= 21 %}
                    <p>${{ nextTwentyK }}</p>
                    {% elseif monthlyConsumption <= 20 and monthlyConsumption >= 11 %}
                    <p>${{ nextTenK }}</p>
                {% elseif monthlyConsumption <= 10 %}
                    <p>${{ firstTenK }}</p>
                {% endif %}
            
            {% elseif customer == 'Non-Residential' %}
                {# Non-Residential Monthly Water Volume Charges
                ========================= #}
                {% set baseFee = 10.35 %} {# using 3/4 inch Meter Size as Default Price for Non-Residential #}
                {% set allUsageVol = 2.11 %}

                {# Non-Residential Math Calculation
                ========================= #}
                {% set allUsage = (monthlyConsumption * allUsageVol) + baseFee|number_format(2) %}
                <p>${{ allUsage }}</p>

            {% elseif customer == 'Landscape' %}
                {# Landscape Monthly Water Volume Charges
                ========================= #}
                {% set baseFee = 10.35 %} {# using 3/4 inch Meter Size as Default Price for Landscape #}
                {% set allUsageVol = 2.55 %}

                {# Landscape Math Calculation
                ========================= #}
                {% set allUsage = (monthlyConsumption * allUsageVol) + baseFee|number_format(2) %}
                <p>${{ allUsage }}</p>

            {% elseif customer == 'Industrial' %}
                {# Industrial Monthly Water Volume Charges
                ========================= #}
                {% set baseFee = 10.35 %} {# using 3/4 inch Meter Size as Default Price for Industrial #}
                {% set allUsageVol = 2.08 %}

                {# Industrial Math Calculation
                ========================= #}
                {% set allUsage = (monthlyConsumption * allUsageVol) + baseFee|number_format(2) %}
                <p>${{ allUsage }}</p>
                
            {% elseif customer == 'Reclaimed' %}
                {# Reclaimed Monthly Water Volume Charges
                ========================= #}
                {% set baseFee = 10.35 %} {# using 3/4 inch Meter Size as Default Price for Reclaimed #}
                {% set allUsageVol = 0.69 %}

                {# Reclaimed Math Calculation
                ========================= #}
                {% set allUsage = (monthlyConsumption * allUsageVol) + baseFee|number_format(2) %}
                <p>${{ allUsage }}</p>
            {% endif %}
        </div>
      '#ajax': true
  row_5:
    '#type': flexbox
    new_water_bill:
      '#type': computed_twig
      '#title': 'New Water Bill'
      '#template': |
        <div class="form-text required form-control">
            {# Global Variables
            ================================================== #}
            {% set customer = data.customer_type %}
            {% set monthlyConsumption = data.monthly_consumption %}

            {# If/Else Statement Based on Customer Type #}
            {% if customer == 'Residential' %}
                {# Residential Monthly Water Volume Charges
                ========================= #}
                {% set baseFee = 10.35 %} {# using 3/4 inch Meter Size as Default Price for Residential houses #}
                {% set firstTenKVol = 1.60 %}
                {% set nextTenKVol = 2.08 %}
                {% set nextFourtyKVol = 2.62 %}
                {% set overSixtyKVol = 3.27 %}
                {% set proposedRateChange = 1.27 %}

                {# Residential Math Calculation
                ========================= #}
                {% set firstTenK = (monthlyConsumption * firstTenKVol) + baseFee|number_format(2) %}
                {% set nextTenK = ((monthlyConsumption - 10) * nextTenKVol) + (10 * firstTenKVol) + baseFee|number_format(2) %}
                {% set nextFourtyK = ((monthlyConsumption - 20) * nextFourtyKVol) + (10 * nextTenKVol) + (10 * firstTenKVol) + baseFee|number_format(2) %}
                {% set overSixtyK = ((monthlyConsumption - 60) * overSixtyKVol) + (40 * nextFourtyKVol) + (10 * nextTenKVol) + (10 * firstTenKVol) + baseFee|number_format(2) %}

                {# If/Else Statement Based on Residential Water Monthly Consumption, 
                    Will Display Math Calculations as New Water Bill
                ========================= #}
                {% if monthlyConsumption >= 61 %}
                    <p>${{ overSixtyK + ((overSixtyK * proposedRateChange) /100)|round(2) }}</p>
                {% elseif monthlyConsumption <= 60 and monthlyConsumption >= 21 %}
                    <p>${{ nextFourtyK + ((nextFourtyK * proposedRateChange) /100)|round(2) }}</p>
                {% elseif monthlyConsumption <= 20 and monthlyConsumption >= 11 %}
                    <p>${{ nextTenK + ((nextTenK * proposedRateChange) /100)|round(2)  }}</p>
                {% elseif monthlyConsumption <= 10 %}
                    <p>${{ firstTenK + ((firstTenK * proposedRateChange) /100)|round(2) }}</p>
                {% endif %}
            
            {% elseif customer == 'Multifamily' %}
                {# Multifamily Monthly Water Volume Charges
                ========================= #}
                {% set baseFee = 29.31 %} {# using 2 inch Meter Size as Default Price for Multifamily houses #}
                {% set firstTenKVol = 0.87 %}
                {% set nextTenKVol = 1.07 %}
                {% set nextTwentyKVol = 1.47 %}
                {% set overFourtyKVol = 2.21 %}
                {% set proposedRateChange = 1.83 %}

                {# Multifamily Math Calculation
                ========================= #}
                {% set firstTenK = (monthlyConsumption * firstTenKVol) + baseFee|number_format(2) %}
                {% set nextTenK = ((monthlyConsumption - 10) * nextTenKVol) + (10 * firstTenKVol) + baseFee|number_format(2) %}
                {% set nextTwentyK = ((monthlyConsumption - 20) * nextTwentyKVol) + (10 * nextTenKVol) + (10 * firstTenKVol) + baseFee|number_format(2) %}
                {% set overFourtyK = ((monthlyConsumption - 40) * overFourtyKVol) + (20 * nextTwentyKVol) + (10 * nextTenKVol) + (10 * firstTenKVol) + baseFee|number_format(2) %}

                {# If/Else Statement Based on Multifamily Water Monthly Consumption
                ========================= #}
                {% if monthlyConsumption >= 41 %}
                    <p>${{ overFourtyK + ((overFourtyK * proposedRateChange) /100)|round(2) }}</p>
                {% elseif monthlyConsumption <= 40 and monthlyConsumption >= 21 %}
                    <p>${{ nextTwentyK + ((nextTwentyK * proposedRateChange) /100)|round(2) }}</p>
                {% elseif monthlyConsumption <= 20 and monthlyConsumption >= 11 %}
                    <p>${{ nextTenK + ((nextTenK * proposedRateChange) /100)|round(2)  }}</p>
                {% elseif monthlyConsumption <= 10 %}
                    <p>${{ firstTenK + ((firstTenK * proposedRateChange) /100)|round(2) }}</p>
                {% endif %}

            {% elseif customer == 'Non-Residential' %}
                {# Non-Residential Monthly Water Volume Charges
                ========================= #}
                {% set baseFee = 10.35 %} {# using 3/4 inch Meter Size as Default Price for Non-Residential #}
                {% set allUsageVol = 2.11 %}
                {% set proposedRateChange = 3.79 %}

                {# Non-Residential Math Calculation
                ========================= #}
                {% set allUsage = (monthlyConsumption * allUsageVol) + baseFee|number_format(2) %}
                <p>${{ allUsage + ((allUsage * proposedRateChange) /100)|round(2) }}</p>

            {% elseif customer == 'Landscape' %}
                {# Landscape Monthly Water Volume Charges
                ========================= #}
                {% set baseFee = 10.35 %} {# using 3/4 inch Meter Size as Default Price for Landscape #}
                {% set allUsageVol = 2.55 %}
                {% set proposedRateChange = 5.88 %}

                {# Landscape Math Calculation
                ========================= #}
                {% set allUsage = (monthlyConsumption * allUsageVol) + baseFee|number_format(2) %}
                <p>${{ allUsage + ((allUsage * proposedRateChange) /100)|round(2) }}</p>

            {% elseif customer == 'Industrial' %}
                {# Industrial Monthly Water Volume Charges
                ========================= #}
                {% set baseFee = 10.35 %} {# using 3/4 inch Meter Size as Default Price for Industrial #}
                {% set allUsageVol = 2.08 %}
                {% set proposedRateChange = 4.33 %}

                {# Industrial Math Calculation
                ========================= #}
                {% set allUsage = (monthlyConsumption * allUsageVol) + baseFee|number_format(2) %}
                <p>${{ allUsage + ((allUsage * proposedRateChange) /100)|round(2) }}</p>

            {% elseif customer == 'Reclaimed' %}
                {# Reclaimed Monthly Water Volume Charges
                ========================= #}
                {% set baseFee = 10.35 %} {# using 3/4 inch Meter Size as Default Price for Reclaimed #}
                {% set allUsageVol = 0.69 %}
                {% set proposedRateChange = 8.00 %}

                {# Reclaimed Math Calculation
                ========================= #}
                {% set allUsage = (monthlyConsumption * allUsageVol) + baseFee|number_format(2) %}
                <p>${{ allUsage + ((allUsage * proposedRateChange) /100)|round(2) }}</p>
            {% endif %}
        </div>
      '#ajax': true
sewer_container:
  '#type': fieldset
  '#title': Sewer
  row_7:
    '#type': flexbox
    current_wastewater_bill:
      '#type': computed_twig
      '#title': 'Current Wastewater Bill'
      '#template': |
        <div class="form-text required form-control">
            {# Global Variables
            ================================================== #}
            {% set customer = data.customer_type %}
            {% set monthlyConsumption = data.monthly_consumption %} {# Used only Non-Residential #}
            
            {# If/Else Statement Based on Customer Type #}
            {% if customer == 'Residential' %}
                {# Residential Wastewater Charges
                ========================= #}
                {% set baseFee = 27.32 %}

                {# Residential Wastewater Math Calculation
                ========================= #}
                {% set wasteWaterRate = baseFee|number_format(2) %}
                <p>${{ wasteWaterRate }}</p>

            {% elseif data.customer_type == 'Multifamily' %}
                {# Multifamily Wastewater Charges
                ========================= #}
                {% set baseFee = 9.76 %}
                {% set numberOfUnits = data.number_of_units %}

                {# Multifamily Wastewater Math Calculation
                ========================= #}
                {% set wasteWaterRate = baseFee * numberOfUnits|number_format(2) %}
                <p>${{ wasteWaterRate }}</p>

            {% elseif data.customer_type == 'Non-Residential' %}
                {# Non-Residential Wastewater Charges
                ========================= #}
                {% set baseFee = 7.65 %} {# Monthly Base Charge for Non-Residential #}
                {% set volumeFee = 3.49 %} {# Volume Charge Per 1,000 Gallons of Metered Water Use #}

                {# Non-Residential Wastewater Math Calculation
                ========================= #}
                {% set wasteWaterRate = (monthlyConsumption * volumeFee) + baseFee|number_format(2) %}
                <p>${{ wasteWaterRate }}</p>

            {% elseif data.customer_type == 'Industrial' %}
                {# Industrial Wastewater Charges
                ========================= #}
                {% set baseFee = 7.65 %} {# Monthly Base Charge for Non-Residential #}
                {% set volumeFee = 3.49 %} {# Volume Charge Per 1,000 Gallons of Metered Water Use #}

                {# Industrial Wastewater Math Calculation
                ========================= #}
                {% set wasteWaterRate = (monthlyConsumption * volumeFee) + baseFee|number_format(2) %}
                <p>${{ wasteWaterRate }}</p>
            {% endif %}
        </div>
      '#ajax': true
  row_8:
    '#type': flexbox
    new_wastewater_bill:
      '#type': computed_twig
      '#title': 'New Wastewater Bill'
      '#template': |
        <div class="form-text required form-control">
            {# Global Variables
            ================================================== #}
            {% set customer = data.customer_type %}
            {% set monthlyConsumption = data.monthly_consumption %} {# Used only Non-Residential #}

            {# If/Else Statement Based on Customer Type #}
            {% if customer == 'Residential' %}
                {# Residential Wastewater Charges
                ========================= #}
                {% set baseFee = 27.32 %}
                {% set proposedRateChange = 1.21 %}

                {# Residential Wastewater Math Calculation
                ========================= #}
                {% set wasteWaterRate = baseFee|number_format(2) %}
                <p>${{ wasteWaterRate + ((wasteWaterRate * proposedRateChange) /100)|round(2) }}</p>

            {% elseif data.customer_type == 'Multifamily' %}
                {# Multifamily Wastewater Charges
                ========================= #}
                {% set baseFee = 9.76 %}
                {% set numberOfUnits = data.number_of_units %}
                {% set proposedRateChange = 8.30 %}

                {# Multifamily Wastewater Math Calculation
                ========================= #}
                {% set wasteWaterRate = baseFee * numberOfUnits|number_format(2) %}
                <p>${{ wasteWaterRate + ((wasteWaterRate * proposedRateChange) /100)|round(2) }}</p>

            {% elseif data.customer_type == 'Non-Residential' %}
                {# Non-Residential Wastewater Charges
                ========================= #}
                {% set baseFee = 7.65 %} {# Monthly Base Charge for Non-Residential #}
                {% set volumeFee = 3.49 %} {# Volume Charge Per 1,000 Gallons of Metered Water Use #}
                {% set proposedRateChange = 6.88 %}

                {# Non-Residential Wastewater Math Calculation
                ========================= #}
                {% set wasteWaterRate = (monthlyConsumption * volumeFee) + baseFee|number_format(2) %}
                <p>${{ wasteWaterRate + ((wasteWaterRate * proposedRateChange) /100)|round(2) }}</p>

            {% elseif data.customer_type == 'Industrial' %}
                {# Industrial Wastewater Charges
                ========================= #}
                {% set baseFee = 7.65 %} {# Monthly Base Charge for Non-Residential #}
                {% set volumeFee = 3.49 %} {# Volume Charge Per 1,000 Gallons of Metered Water Use #}
                {% set proposedRateChange = 6.88 %}

                {# Industrial Wastewater Math Calculation
                ========================= #}
                {% set wasteWaterRate = (monthlyConsumption * volumeFee) + baseFee|number_format(2) %}
                <p>${{ wasteWaterRate + ((wasteWaterRate * proposedRateChange) /100)|round(2) }}</p>
            {% endif %}
        </div>
      '#ajax': true
solid_waste_container:
  '#type': fieldset
  '#title': 'Solid Waste'
  '#states':
    visible:
      ':input[name="alley_customer"]':
        filled: true
  row_10:
    '#type': flexbox
    current_solid_waste_bill:
      '#type': computed_twig
      '#title': 'Current Solid Waste Bill'
      '#template': |
        <div class="form-text required form-control">
            {# Global Variables
            ================================================== #}
            {% set customer = data.customer_type %}
            {% set hasAlley = data.alley_customer %}

            {# Solid Waste Charges
            ========================= #}
            {% set baseFee = 17.95 %}
            {% set alleyCustomerRate = 1.61 %}

            {# Solid Waste Math Calculation
            ========================= #}
            {% set solidWasteRate = baseFee|number_format(2) %}
            {% set solidWasteRateAlley = baseFee + alleyCustomerRate|number_format(2) %}
            
            {# Conditional Statement Based on whether customer has alley or not #}
            {% if customer == 'Residential' and hasAlley == 'Yes' %}
                <p>${{ solidWasteRateAlley }}</p>
            {% elseif customer == 'Residential' %}
                <p>${{ solidWasteRate }}</p>
            {% endif %}
        </div>
      '#ajax': true
  row_11:
    '#type': flexbox
    new_solid_waste_bill:
      '#type': computed_twig
      '#title': 'New Solid Waste Bill'
      '#template': |
        <div class="form-text required form-control">
            {# Global Variables
            ================================================== #}
            {% set customer = data.customer_type %}
            {% set hasAlley = data.alley_customer %}

            {# Solid Waste Charges
            ========================= #}
            {% set baseFee = 17.95 %}
            {% set alleyCustomerRate = 1.61 %}
            {% set proposedRateChange = 3.40 %}

            {# Solid Waste Math Calculation
            ========================= #}
            {% set solidWasteRate = baseFee|number_format(2) %}
            {% set solidWasteRateAlley = baseFee + alleyCustomerRate|number_format(2) %}
            
            {# Conditional Statement Based on whether customer has alley or not #}
            {% if customer == 'Residential' and hasAlley == 'Yes' %}
                <p>${{ solidWasteRateAlley + ((solidWasteRateAlley * proposedRateChange) /100)|round(2) }}</p>
            {% elseif customer == 'Residential' %}
                <p>${{ solidWasteRate + ((solidWasteRate * proposedRateChange) /100)|round(2) }}</p>
            {% endif %}
        </div>
      '#ajax': true
row_12:
'#type': flexbox
final_current_bill:
  '#type': computed_twig
  '#title': 'Final Current Bill'
  '#template': '{{ data.current_water_bill + data.current_wastewater_bill + data.current_solid_waste_bill|number_format(2) }}'
  '#ajax': true
final_new_bill:
  '#type': computed_twig
  '#title': 'Final New Bill'
  '#template': '{{ data.new_water_bill + data.new_wastewater_bill + data.new_solid_waste_bill }}'
  '#ajax': true
4uk4 avatar
cn flag
Similar question https://drupal.stackexchange.com/questions/300862/total-of-multiple-computed-elements. But you have an error message that they didn't mention. To get rid of that you could try the `|render` filter to convert the markup object to a string. Then you can try to append other twig filters to get rid of html tags and currency signs until the string evaluates to a number.
rosendo.fig avatar
in flag
@4uk4 thanks for sharing the above example
Score:0
id flag

Try passing the values through the |trim filter first, to cast them to strings.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.