You are here

public function PackageTypeForm::form in Commerce Shipping 8.2

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/Form/PackageTypeForm.php, line 14

Class

PackageTypeForm

Namespace

Drupal\commerce_shipping\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);

  /** @var \Drupal\commerce_shipping\Entity\PackageTypeInterface $package_type */
  $package_type = $this->entity;
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#maxlength' => 255,
    '#default_value' => $package_type
      ->label(),
    '#required' => TRUE,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $package_type
      ->id(),
    '#machine_name' => [
      'exists' => '\\Drupal\\commerce_shipping\\Entity\\PackageType::load',
    ],
    '#disabled' => !$package_type
      ->isNew(),
  ];
  $form['dimensions'] = [
    '#type' => 'physical_dimensions',
    '#title' => $this
      ->t('Dimensions'),
    '#default_value' => $package_type
      ->getDimensions(),
    '#required' => TRUE,
  ];
  $weight = $package_type
    ->getWeight();
  $form['weight'] = [
    '#type' => 'physical_measurement',
    '#title' => $this
      ->t('Weight'),
    '#measurement_type' => MeasurementType::WEIGHT,
    '#default_value' => $weight ?: [
      'number' => 0,
      'unit' => 'g',
    ],
    '#required' => TRUE,
  ];
  return $form;
}