Drupal 8: Form API - Weight Fields

Michalak Drupal Commerce Developer

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'], 


Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.