Physical Fields
Provides field types for storing and manipulating physical measurements with support for multiple units, automatic conversions, and precision arithmetic.
physical
Install
composer require 'drupal/physical:8.x-1.5'
composer require 'drupal/physical:8.x-1.1'
Overview
Physical Fields is a comprehensive Drupal module that provides robust field types for storing physical measurements and their associated units of measure. It supports six distinct measurement types: Area, Length, Temperature, Volume, Weight, and Pressure, each with multiple units and automatic conversion capabilities.
The module offers two primary field types: Measurement (for single values like weight or temperature) and Dimensions (for three-dimensional measurements like product packaging). All numeric operations use bcmath-based arithmetic to prevent floating-point precision loss, making it suitable for e-commerce and scientific applications.
A key feature is language-specific number input and display, allowing users to enter numbers using their locale's decimal separator (e.g., comma in French) while storing data in a standardized format. The module provides a complete API with value objects that support arithmetic operations, comparisons, and unit conversions programmatically.
Features
- Six measurement types: Area, Length, Temperature, Volume, Weight, and Pressure with comprehensive unit support
- Two field types: Measurement (single value + unit) and Dimensions (length × width × height + unit)
- Automatic unit conversion with precise bcmath-based arithmetic to prevent floating-point errors
- Language-specific number input and formatting using PHP intl extension
- Immutable value objects with arithmetic operations (add, subtract, multiply, divide)
- Comparison methods for measurements (equals, greaterThan, lessThan, compareTo)
- Configurable widget settings for default unit, unit change allowance, and available units
- Formatter with output unit conversion for displaying measurements in preferred units
- Custom form elements for measurement and dimension input
- Migration support for Drupal 7 weight fields
- Field UI integration with preconfigured options for each measurement type
Use Cases
E-commerce Product Weight and Dimensions
Store product weight and shipping dimensions for commerce applications. The module integrates well with Drupal Commerce, providing precise weight calculations for shipping rate determination. Configure weight fields for products and dimension fields for package sizes. Use formatter output_unit settings to display weights in customer-preferred units.
Scientific Data Collection
Record measurements with high precision for scientific applications. The bcmath-based arithmetic ensures no floating-point errors accumulate during calculations. Temperature fields support Kelvin, Celsius, and Fahrenheit with accurate conversion formulas.
Real Estate Property Measurements
Store property dimensions, room sizes, and land areas. Use Area fields for lot sizes (supporting hectares, square meters, square feet) and Dimension fields for room measurements. Formatters can convert between metric and imperial units for international listings.
Manufacturing and Inventory
Track physical specifications of manufactured goods or inventory items. Store product dimensions for warehouse management, weight for shipping logistics, and volume for container packing optimization.
Recipe and Nutrition Applications
Store ingredient quantities with volume (ml, l, cups) or weight (g, kg, oz) measurements. The conversion capabilities allow recipes to be displayed in either metric or imperial units based on user preference.
Pressure Monitoring Systems
Record pressure measurements from sensors or equipment in various units (psi, bar, Pa, atm). Convert between units for different regional standards or equipment specifications.
Tips
- Use the MeasurementType constants (MeasurementType::WEIGHT, etc.) when working with measurement types programmatically
- Always pass numbers as strings to the Calculator class and value objects to maintain precision
- The Temperature class uses custom conversion formulas rather than simple multiplication factors due to the non-linear nature of temperature scales
- Install the PHP intl extension for proper locale-specific number formatting; without it, users must use period as decimal separator
- Value objects are immutable - arithmetic operations return new instances rather than modifying the original
- Use toMeasurement() method on MeasurementItem field items to get a value object for programmatic manipulation
- The physical_number form element automatically shows a placeholder indicating the expected decimal format for the current locale
- When migrating from Drupal 7, use the physical_weight migrate process plugin to transform field values
Technical Details
Troubleshooting 4
Install the PHP intl extension. Without it, the NumberFormatter falls back to basic functionality without locale-specific decimal separators.
Ensure you're using the Calculator class or value object methods for arithmetic. Never use PHP's native float arithmetic with measurement values.
Temperature conversion uses special formulas (not simple multiplication). The Temperature class overrides the convert() method to handle Kelvin/Celsius/Fahrenheit conversions correctly.
The physical_number element handles locale parsing automatically. Ensure you're using the physical_measurement or physical_dimensions form elements rather than plain textfields.