Skip to main content
  1. Posts/

Drupal 8: Form API - Weight Fields

·80 words·1 min·
Drupal Drupal 8 Form API Drupal Commerce Drupal Development Purchase Orders

Currently, I am working on a custom form that generates products on the fly for a custom purchase order implementation. 

For this Drupal 8 commerce website, the product variations have a required weight field which was needed in the custom form. 

Here is an example of the weight field.

    $form['product']['weight'] = array(
      '#type' => 'physical_measurement',
      '#title' => t('Weight'),
      '#measurement_type' => 'weight',
       //the default field is required and must be an array.
      '#default_value' => ['number' => 2.00, 'unit' => 'oz'], 
    );
Michael Michalak
Author
Michael Michalak
Experienced Drupal consultant and Fractional CTO with over 13 years of expertise in backend development, optimization, and securing Drupal applications, specializing in custom module development, migrations, and enterprise integrations across various industries.