Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I need to add some fields than can take numeric values and enable Drupal to calculate it. I chose an integer field, tried to do some simple math, but Drupal 7 treats values of integer field as text. Moreover, integer field type widget can be only set up as text. Can anybody help me, and tell me how to add field with number values?

share|improve this question

3 Answers

The reason why the widget for an Integer field is a text field is that the form element used from a browser is a text field. This means the browser is always returning text for an integer field.

This should not be a problem, since:

  • When you add an Integer field to an entity, the field value is validated to be an integer; an input containing characters that are not digits (e.g. 2A, Q3, 2.0) is not accepted, and the users get an error message requiring them to enter only numbers for that field.

  • PHP generally accepts a string containing only digits instead of an integer without to report errors. If you execute the following code, you would get 15 as result. (It requires the Devel module.)

    $a = "3";
    $b = "6";
    $c = "2";
    
    dpm($a + $b * $c);
    

    You would get the same result if $a is set to "3A".

share|improve this answer

By using Computed Field module you can achieve your goal, You need to have a good working knowledge of PHP and the Drupal API to use this module.

share|improve this answer

I maintain a small module called SpinInt on drupal.org which allows you to specify a scrolling integer widget in your field setup. Not sure if that's any interest but it's a bit friendlier than a text field I guess.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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