I am storing a value (number 1-5) in a drop down field and would like to do a calculation based on the numbers filled in. The only way I could get my calculation to work was to store the number (1-5) in a number field (not a drop down field) so I'm wondering if it's because the drop down content is seen as a string and not an integer. If so, perhaps I need to convert the string to an integer. Although I have it working with the value (1-5) stored in a number field, I'd like to understand how I could make it work with a drop down. I saw another post that sounded similar: How can I convert a dropdown field value to an integer within a hash?

Here is my template code that works with my number field:

<!-- AORTA -->
    <!-- Get entries for study type Aorta for logged in sonographer. Filter out any scores that are default zero. -->
        {% set imageQualityScoreAorta = craft.entries.section('logsQA').logGeneral_statusWorkflow('reviewed').search('logGeneral_studyType:aorta -logReviewer_imageQualityNumberTest:0').relatedTo(
            { targetElement: currentUser, field: 'logGeneral_sonographerName' }
        ) %}

    <!-- Get average image quality score IF there are any -->
        {% if imageQualityScoreAorta|length %}
            <!-- Set default numbers -->
            {% set additionAorta = 0 %}
            {% set countAorta = 0 %}

            <!-- Loop -->
            {% for entry in imageQualityScoreAorta %}
                {% set additionAorta = additionAorta + entry.logReviewer_imageQualityNumberTest %}</td>
                {% set countAorta = loop.index %}
            {% endfor %}

            <!-- Get average image quality score -->
            {% set averageValueAorta = (additionAorta / countAorta)|round(1, 'floor') %}
        {% endif %}

This is the output:

<!-- Current User Total Studies -->
<table class="table">
            <td class="c-font-uppercase c-font-bold" scope="row">Aorta</th>
            {% if imageQualityScoreAorta|length %}
                <td class="c-center c-font-bold">{{ averageValueAorta }}</td>
            {% else %}
                <td class="c-center c-font-bold">N/A</td>
            {% endif %}

1 Answer 1


When working with a dropdown in Twig, it's important to remember that you're dealing with an object (not a simple string).

This is an object:
{{ entry.myDropdown }}

It's easy to forget, however, because Craft works a little magic behind the scenes. There are many objects (including this one) which have an internal __toString behavior. This is called a "magic method", because it will automatically convert the value to a string when you use it as a string.

But in your case, you're not directly outputting it as a string. You're trying to do math with the object, and the object isn't really sure how to respond to that. So nothing gets converted, and your object remains mathematically useless.

Instead, you'll need to append .value to your object. This will give you the selected value of your dropdown, in a format that you can use.

This is the dropdown's selected value:
{{ entry.myDropdown.value }}
  • 1
    SO HELPFUL! I've been so confused by this until this answer. Jun 15, 2018 at 2:23

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.