physical_quantity_fields
The Physical Quantity Fields module for Drupal 10/11 is a robust engineering and scientific toolkit that provides 15+ specialized field types for physical measurements. Built with a service-based architecture and NIST-sourced conversion factors, it ensures high-precision data storage and flexible display options for everything from microscopic lengths to astronomical distances.
Key Features
- 15 Specialized Field Types: Comprehensive support for Length, Mass, Temperature, Volume, Time, Speed, Area, Pressure, Energy, Power, Angle, and Data Storage.
- Scientific Precision: Utilizes NIST-sourced conversion factors and mathematically correct formulaic temperature conversions (Celsius, Fahrenheit, Kelvin, etc.) to maintain round-trip accuracy.
- Granular Formatter Control: Customize displays with 0–10 decimal precision, prefix/suffix toggles, text transformations (UPPERCASE/Title Case), and trailing dot options for abbreviations.
- Developer-First Architecture: Logic is centralized in the
UnitConverterservice. Easily inject conversion capabilities into your own custom modules or services. - Themeable Output: Includes built-in Twig template suggestions (e.g.,
physical-quantity--length.html.twig) for effortless per-type styling. - Legacy Compatible: Fully backward compatible with 1.x data. Includes automated configuration migrations for a seamless upgrade path.
Installation
Install the module via Composer to automatically handle class mapping and service registration:
composer require drupal/physical_quantity_fieldsEnable the module using Drush:
drush en physical_quantity_fieldsConfiguration & Usage
Once enabled, physical quantity fields can be added to any fieldable entity (Nodes, Users, Media, etc.):
- Navigate to Structure » Content types » [Your Type] » Manage fields.
- Click Add field and select a quantity type (e.g., Mass or Energy).
- In Manage form display, configure unit-label suffixes and min/max validation constraints.
- In Manage display, choose your target display unit. The module handles the conversion from the stored base unit automatically.
Developer API
For developers needing to perform conversions in code, the physical_quantity_fields.unit_converter service provides a clean interface:
// Convert 100 Kilometers to Miles $miles = $unitConverter->convert(100.0, 'km', 'mi', 'length');
Requirements
- Drupal: 10.2+ or 11.x
- PHP: 8.1+
- Core Module: Field
Similar Projects
Supporting this Module
The Physical Quantity Fields module is maintained to provide the Drupal community with a reliable standard for measurement data. You can help improve the project:
- Bug Reports: Report accuracy issues or UI bugs in the Issue Queue.
- New Units: Suggest additional units or quantity types (e.g., Luminosity or Frequency) via a feature request.
- Testing: The module includes Unit, Kernel, and Functional tests. Contributions to test coverage are always welcome!