commit 3f5901e85d5e168b99e6e2a8344e836e1b08b3fe
Author: Jonathan Sacksick <jonathan.sacksick@gmail.com>
Date:   Sun Mar 5 17:10:00 2017 +0200

    Prevent deletion/updating already in use package types.

diff --git a/src/Entity/PackageType.php b/src/Entity/PackageType.php
index 3591522..ba53d27 100644
--- a/src/Entity/PackageType.php
+++ b/src/Entity/PackageType.php
@@ -21,6 +21,7 @@ use Drupal\physical\WeightUnit;
  *     plural = "@count package types",
  *   ),
  *   handlers = {
+ *     "access" = "Drupal\commerce_shipping\PackageTypeAccessControlHandler",
  *     "list_builder" = "Drupal\commerce_shipping\PackageTypeListBuilder",
  *     "form" = {
  *       "add" = "Drupal\commerce_shipping\Form\PackageTypeForm",
@@ -85,6 +86,19 @@ class PackageType extends ConfigEntityBase implements PackageTypeInterface {
   /**
    * {@inheritdoc}
    */
+  public function isUsed() {
+    if ($this->isNew()) {
+      return FALSE;
+    }
+    $query = \Drupal::entityQuery('commerce_shipment');
+    $query->condition('package_type', 'commerce_package_type:' . $this->uuid());
+    $shipment_ids = $query->execute();
+    return !empty($shipment_ids);
+  }
+
+  /**
+   * {@inheritdoc}
+   */
   public function getDimensions() {
     return $this->dimensions;
   }
diff --git a/src/Entity/PackageTypeInterface.php b/src/Entity/PackageTypeInterface.php
index 76dff6f..833e133 100644
--- a/src/Entity/PackageTypeInterface.php
+++ b/src/Entity/PackageTypeInterface.php
@@ -10,6 +10,14 @@ use Drupal\Core\Config\Entity\ConfigEntityInterface;
 interface PackageTypeInterface extends ConfigEntityInterface {
 
   /**
+   * Determines whether the package type is already in use.
+   *
+   * @return bool
+   *   TRUE if this package type is already referenced by shipments.
+   */
+  public function isUsed();
+
+  /**
    * Gets the package type dimensions.
    *
    * @return array
diff --git a/src/Form/PackageTypeForm.php b/src/Form/PackageTypeForm.php
index d7a7012..1527e08 100644
--- a/src/Form/PackageTypeForm.php
+++ b/src/Form/PackageTypeForm.php
@@ -4,6 +4,7 @@ namespace Drupal\commerce_shipping\Form;
 
 use Drupal\Core\Entity\EntityForm;
 use Drupal\Core\Form\FormStateInterface;
+use Drupal\Core\Render\Element;
 use Drupal\physical\MeasurementType;
 
 class PackageTypeForm extends EntityForm {
@@ -11,6 +12,23 @@ class PackageTypeForm extends EntityForm {
   /**
    * {@inheritdoc}
    */
+  public function buildForm(array $form, FormStateInterface $form_state) {
+    $form = parent::buildForm($form, $form_state);
+
+    // If this package type is already in use, disable all the form elements.
+    if ($this->entity->isUsed()) {
+      foreach (Element::children($form) as $child) {
+        $form[$child]['#disabled'] = TRUE;
+      }
+      drupal_set_message($this->t('This package type cannot be modified until all referencing shipments are deleted.'), 'warning');
+    }
+
+    return $form;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
   public function form(array $form, FormStateInterface $form_state) {
     $form = parent::form($form, $form_state);
     /** @var \Drupal\commerce_shipping\Entity\PackageTypeInterface $package_type */
