You are here

protected function Shipment::prepareFields in Commerce Shipping 8.2

Ensures that the package_type and weight fields are populated.

2 calls to Shipment::prepareFields()
Shipment::populateFromProposedShipment in src/Entity/Shipment.php
Populates the shipment from the given proposed shipment.
Shipment::preSave in src/Entity/Shipment.php
Acts on an entity before the presave hook is invoked.

File

src/Entity/Shipment.php, line 511

Class

Shipment
Defines the shipment entity class.

Namespace

Drupal\commerce_shipping\Entity

Code

protected function prepareFields() {
  if (empty($this
    ->getPackageType()) && !empty($this
    ->getShippingMethodId())) {
    $shipping_method = $this
      ->getShippingMethod();
    if ($shipping_method) {
      $default_package_type = $shipping_method
        ->getPlugin()
        ->getDefaultPackageType();
      $this
        ->set('package_type', $default_package_type
        ->getId());
    }
  }
  $this
    ->recalculateWeight();
}