You are here

public function PackageTypeManager::processDefinition in Commerce Shipping 8.2

Performs extra processing on plugin definitions.

By default we add defaults for the type to the definition. If a type has additional processing logic they can do that by replacing or extending the method.

Overrides DefaultPluginManager::processDefinition

File

src/PackageTypeManager.php, line 67

Class

PackageTypeManager
Manages discovery and instantiation of package type plugins.

Namespace

Drupal\commerce_shipping

Code

public function processDefinition(&$definition, $plugin_id) {
  parent::processDefinition($definition, $plugin_id);
  $definition['id'] = $plugin_id;
  foreach ([
    'remote_id',
    'label',
    'dimensions',
  ] as $required_property) {
    if (empty($definition[$required_property])) {
      throw new PluginException(sprintf('The package_type "%s" must define the "%s" property.', $plugin_id, $required_property));
    }
  }
  foreach ([
    'length',
    'width',
    'height',
    'unit',
  ] as $dimension_property) {
    if (empty($definition['dimensions'][$dimension_property])) {
      throw new PluginException(sprintf('The package type "%s" property "dimensions" must have a "%s" key.', $plugin_id, $dimension_property));
    }
  }
  if (isset($definition['weight'])) {
    foreach ([
      'number',
      'unit',
    ] as $weight_property) {
      if (!isset($definition['weight'][$weight_property])) {
        throw new PluginException(sprintf('The package type "%s" property "weight" must have a "%s" key.', $plugin_id, $weight_property));
      }
    }
  }
  else {

    // Package types should have a weight value even if they're weightless.
    $definition['weight'] = [
      'number' => 0,
      'unit' => WeightUnit::GRAM,
    ];
  }
}